天天看点

iOS 玩转CocoaPods

####导语:

有时候看到其他人 source开源时候用pod xxx 配置在你的Podfile文件中,执行下pod install 或者 pod update ,代码瞬间就到你的pod库, 顿时觉得高大上。那是怎么做到的呢?

Agenda:

  • CocoaPods 的由来
  • Github 使用
  • PodSpec介绍
  • PodSpec上传
  • 遇到的坑及解决方案

###一,CocoaPods 的由来 Android app目前通过gradle来管理和配置你的source,比如需要用到Eventbus只要在build.gradle中配置下

dependencies {
    compile 'org.greenrobot:eventbus:3.0.0'
}
复制代码
           

iOS必须要有类似的神器啊:CocoaPod就是这把神器. 在CocoaPod没出来之前iOS要用引用第三库的做法如下: 比如引用AFNetWorking库,需要去下载源码,然后需要配置对应的编译环境等。当AFNetWorking库升级,所有过程又来一遍。过程太过复杂啦。 CocoaPods因上面的原因应运而生,它目前是iOS最有名的类库管理工具了,通过cocoaPods,只需要一行命令就可以完全解决,当然前提是你必须正确设置它。目前绝大部分有名的开源类库,都支持CocoaPods。所以,作为iOS程序员的我们,掌握CocoaPods的使用是必不可少的基本技能了。

如果你mac上没有安装pod , sudo gem install cocoapods 具体怎么安装可以参考如下链接: http://www.jianshu.com/p/9e4e36ba8574

开发iOS应用用到pod 的主要命令如下:

pod help-->查看pod命令的帮助
pod search --> Search for pods(搜索pod)
pod trunk   -->   Interact with the CocoaPods API (e.g. publishing new specs) 上传source到trunk
pod spec     -->     Manage pod specs//管理pod spec
pod install  -->Install project dependencies according to versions from a  Podfile.lock //安装项目中的依赖项目
pod update -->Update outdated project dependencies and create new Podfile.lock//跟新依赖项目并且更新Podfile.lock
pod init -->Generate a Podfile for the current directory//创建pod file文件
复制代码
           

其中Podfile.lock的扮演的角色非常重要,具体作用可以参考如下链接 http://m.blog.csdn.net/muzhenhua/article/details/45174071

####二,Github 使用 为嘛要介绍GitHub呢?CocoaPods只是做为项目的具体管理者,podspec文件就放在cocoapod官网上,供大家搜索。但是实际源码则是存储在Github上,那怎么使用Github就非常关键啦。

  • 创建项目 登录你的Github,然后去创建一个新的仓库,如下图创建GKFramework参考
  • 上传项目 下载该仓库,通过git clone 。clone一个仓库下来 怎么clone如下图clone with https.

在终端输入

//git clone后面的是你对应的git 地址
git clone https://github.com/wongstar/GKFramework.git

复制代码
           

然后在这个仓库中修改或者添加你需要对应类或者文件等。

然后通过下面命令

//add 所有的到仓库
git add *
//提交commit信息
git commit
//提交本地到远端
git push origin master
复制代码
           
  • 打tag tag是后续spec中需要用到,以后升级至需要升级对应tag.
//获取当前有多少tag
git tag
//创建tag 0.0.1版本
git tag 0.0.1
复制代码
           
  • update tag到Github上 上传tag到Github服务器上,这个比较简单
git  push origin 0.0.1
复制代码
           
  • 查看tag 如下图所示 :点击branch 然后查看Tags栏目

至此源码已经上传到Github服务器上去了,但是Pod服务器上目前还没有对应的描述,下面接着介绍PodSpec,以及如何上传到cocoapod服务器上去. ####三,PodSpec介绍

在mac 上创建一个podspec,在Terminal终端上输入下面命令:

//注GKFramework.podspec是你的框架名称
pod spec create GKFramework.podspec
复制代码
           

然后编辑podspec文件。如下GKFramework.podspec

Pod::Spec.new do |s|
  s.name         = "GKFramework" //定义名称
  s.version      = "0.0.5" //tag版本
  s.summary      = "A Sample.so you can use it" //简介,以后search到简介
  s.description  = <<-DESC
                   this is gk framework, use it for test your framework. we can use it as framework.
                   DESC
//s.description 定义具体的描述
  s.homepage     = "https://github.com/wongstar/GKFramework"
  s.license      = { :type => "MIT", :file => "LICENSE" }//具体license
  s.author             = { "wongstar" => "[email protected]" }
  s.platform     = :ios, "8.0"//build的平台
  s.ios.deployment_target = "7.0"//最低开发

  s.source       = { :git => "https://github.com/wongstar/GKFramework.git", :tag => "#{s.version}" }
  s.source_files  = 'Classes/**/*'
  #s.public_header_files='GKFramework/Classes/**/*.h'



end

复制代码
           
s.description  = <<-DESC
                   this is gk framework, use it for test your framework. we can use it as framework.
                   DESC
s.description定义了描述该pod是用来做什么的。注意这里的写法

复制代码
           

s.description格式要求必须是下面的这样描述 <<-DESC 这里面你定义的描述.必须用这个格式 DESC

s.source       = { :git => "https://github.com/wongstar/GKFramework.git", :tag => "#{s.version}" }
复制代码
           

必须定义s.source,git链接必须是你上传过的source, tag定义为你在github上对source打的tag.

s.source_files  = 'Classes/**/*' 定义为:Classes目录下的所有文件
复制代码
           
s.dependency:依赖库,不能依赖未发布的库
eg: s.dependency  = 'AFNetworking'
复制代码
           

####四,PodSpec上传

  • 在 cocoapods 注册
//email代表你的email,username代表你的用户名
 pod trunk register email "username"
复制代码
           

执行完上面的命令,你的邮箱会收到一封确认信,点击确认验证一下就ok啦。

  • 判断podspec正确行?
//GKFramework.podspec为你对应的podspec文件
pod  spec lint GKFramework.podspec
复制代码
           

如果是正确的spec会出现下面的提示:

  • 上传到cocoapod服务器
//注:GKFramework.podspec为你对应spec的名称
pod trunk push GKFramework.podspec
复制代码
           

上传成功如下图所示:

  • search 你的库. 网址为:https://cocoapods.org/ 如图五:search GKFramework ####五,遇到的坑及解决方案
  • source file没找到 [iOS] file patterns: The

    source_files

    pattern did not match any file. 确保你的source file是否配置正确,如你的spec目录和source对应的关系
  • cocoapods环境问题 unknown: Encountered an unknown error (Simulator iPhone 4s is not available.) during validation 执行下面命令
sudo gem install cocoapods --pre
复制代码
           

如果执行上面的有问题出现 ERROR: While executing gem ... (Errno::EPERM) Operation not permitted - /usr/bin/xcodeproj 执行下面命令

sudo gem install -n /usr/local/bin cocoapods
复制代码
           
  • Swift 版本问题 [!] The validator for Swift projects uses Swift 3.0 by default, if you are using a different version of swift you can use a .swift-version file to set the version for your Pod. For example to use Swift 2.3, run:
echo "2.3" > .swift-version.
复制代码
           

验证失败,会出现一系列错误,但也不是无根可寻,其中出现错误频率最多的提示是

  • source files没找到 ERROR | [iOS] file patterns: The

    source_files

    pattern did not match any file.

此错误的原因是没有找到匹配的文件。

解决方案: 手动创建文件,具体操作方法如下 终端输入: open /Users/icepoint/Library/Caches/CocoaPods/Pods/External/GKFramework/035cb9aa62b9d49f904fad1119b83da4-aebfe 进入相应文件夹,创建文件夹与source_files文件路径对应 GKFramework/GKFramework/Classes 文件结构如下: GKFramework └── 035cb9aa62b9d49f904fad1119b83da4-aebfe

├── GKFramework │ └── GKFramework │ └──Classes └── LICENSE #开源协议 默认MIT

Classes文件夹存放自己的库文件

  • pod search GKFramework 搜索不到 Unable to find a pod with name, author, summary, or description matching GKFramework. 解决办法: 1.pod install --repo-update 2.或者全部删除:使用命令:rm ~/Library/Caches/CocoaPods/search_index.json 重新search GKFramework