目录
CocoaPods简介
一、CocoaPods 的安装
1、更换 Gem 的源
2、更新升级 Gem 版本
3、安装CocoaPods
4、更换repo镜像为国内服务器二、CocoaPods 的使用(图和文字有细微的出入)
1、新建 Podfile 文件
2、编辑 Podfile 文件
3、执行导入命令
4、第三方库更新三、遭遇到的问题
四、原理和说明
五、更多
1、pod install提速
2、关于Podfile文件编辑时,第三方库版本号的各种写法
CocoaPods简介
每种语言发展到一个阶段,就会出现相应的依赖管理工具,例如Java语言的Maven,nodejs的npm。随着iOS开发者的增多,业界也出现了为iOS程序提供依赖管理的工具,它的名字叫做:CocoaPods。
CocoaPods项目的源码在Github上管理。该项目开始于2011年8月12日,经过多年发展,现在已经成为iOS开发事实上的依赖管理标准工具。开发iOS项目不可避免地要使用第三方开源库,CocoaPods的出现使得我们可以节省设置和更新第三方开源库的时间。在我开发猿题库客户端时,其使用了24个第三方开源库。在没有使用CocoaPods以前,我需要:
把这些第三方开源库的源代码文件复制到项目中,或者设置成git的submodule。
对于这些开源库通常需要依赖系统的一些framework,我需要手工地将这些framework一一增加到项目依赖中,比如通常情况下,一个网络库就需要增加以下framework: CFNetwork, SystemConfiguration, MobileCoreServices, CoreGraphics, zlib。
对于某些开源库,我还需要设置-licucore或者 -fno-objc-arc等编译参数管理这些依赖包的更新。
这些体力活虽然简单,但毫无技术含量并且浪费时间。在使用CocoaPods之后,我只需要将用到的第三方开源库放到一个名为Podfile的文件中,然后执行pod install。CocoaPods就会自动将这些第三方开源库的源码下载下来,并且为我的工程设置好相应的系统依赖和编译参数。
一、CocoaPods 的安装
CocoaPods是用Ruby实现的,要想使用它首先需要有Ruby的环境。幸运的是OS X系统默认的已经可以运行Ruby了,因此我们只需要执行以下命令。
1、更换 Gem 的源
Gem是一个管理Ruby库和程序的标准包,它通过Ruby Gem(如 http://rubygems.org/)源来查找、安装、升级和卸载软件包
<pre class="hljs objectivec" name="code" style="white-space: pre-wrap; word-wrap: normal; padding: 9.5px; color: rgb(101, 123, 131); font-family: Menlo, Monaco, Consolas, 'Courier New', monospace; font-size: 13px; margin-top: 0px; margin-bottom: 20px; line-height: 20px; word-break: break-all; overflow: auto; background: rgb(253, 246, 227);"><code class="objectivec" style="padding: 0px; font-family: Menlo, Monaco, Consolas, 'Courier New', monospace; font-size: 12px; border: none; background-color: transparent;"><span class="hljs-comment" style="margin: 0px; padding: 0px; color: rgb(147, 161, 161);">// 1.移除掉原有的源(服务器在国外,速度较慢)</span> - gem sources --remove https:<span class="hljs-comment" style="margin: 0px; padding: 0px; color: rgb(147, 161, 161);">//rubygems.org/</span> <span class="hljs-comment" style="margin: 0px; padding: 0px; color: rgb(147, 161, 161);">// 2.等有反应之后再敲入以下命令(添加国内的源,原先是 http://ruby.taobao.org/ ,现在淘宝上的ruby镜像换成https了)</span> - gem sources -a https:<span class="hljs-comment" style="margin: 0px; padding: 0px; color: rgb(147, 161, 161);">//ruby.taobao.org/</span> <span class="hljs-comment" style="margin: 0px; padding: 0px; color: rgb(147, 161, 161);">// 3.验证是否替换成功</span> - gem sources -l</code>
2、更新升级 Gem 版本
Gem是管理Ruby库和程序的标准包,如果它的版本过低也可能导致安装失败,解决方案自然是升级Gem,执行下述命令即可:
<pre class="hljs sql" name="code" style="white-space: pre-wrap; word-wrap: normal; padding: 9.5px; color: rgb(101, 123, 131); font-family: Menlo, Monaco, Consolas, 'Courier New', monospace; font-size: 13px; margin-top: 0px; margin-bottom: 20px; line-height: 20px; word-break: break-all; overflow: auto; background: rgb(253, 246, 227);"><code class="sql" style="padding: 0px; font-family: Menlo, Monaco, Consolas, 'Courier New', monospace; font-size: 12px; border: none; background-color: transparent;">// 更新升级gem
- sudo gem <span class="hljs-operator" style="margin: 0px; padding: 0px;"><span class="hljs-keyword" style="margin: 0px; padding: 0px; color: rgb(133, 153, 0);">update</span> <span class="hljs-comment" style="margin: 0px; padding: 0px; color: rgb(147, 161, 161);">--system</span></span></code>
3、安装CocoaPods
<pre class="hljs sql" name="code" style="white-space: pre-wrap; word-wrap: normal; padding: 9.5px; font-family: Menlo, Monaco, Consolas, 'Courier New', monospace; font-size: 13px; margin-top: 0px; margin-bottom: 20px; line-height: 20px; word-break: break-all; overflow: auto; background: rgb(253, 246, 227);"><code class="sql" style="padding: 0px; font-family: Menlo, Monaco, Consolas, 'Courier New', monospace; font-size: 12px; border: none; background-color: transparent;"><span style="margin: 0px; padding: 0px; color: rgb(101, 123, 131);">// 安装CocoaPods(OS X 10.11以前)
- sudo gem </span><span class="hljs-operator" style="margin: 0px; padding: 0px;"><span class="hljs-keyword" style="margin: 0px; padding: 0px; color: rgb(133, 153, 0);">install</span><span style="margin: 0px; padding: 0px; color: rgb(101, 123, 131);"> cocoapods
// </span><span style="margin: 0px; padding: 0px; color: rgb(255, 0, 0);">安装CocoaPods(<span class="hljs-number" style="margin: 0px; padding: 0px; color: rgb(0, 102, 102);">10.11</span>后苹果升级了安全策略)</span><span style="margin: 0px; padding: 0px; color: rgb(101, 123, 131);">
- sudo gem </span><span class="hljs-keyword" style="margin: 0px; padding: 0px; color: rgb(133, 153, 0);">install</span><span style="margin: 0px; padding: 0px; color: rgb(101, 123, 131);"> -</span><span class="hljs-keyword" style="margin: 0px; padding: 0px; color: rgb(133, 153, 0);">n</span><span style="margin: 0px; padding: 0px; color: rgb(101, 123, 131);"> /usr/</span><span class="hljs-keyword" style="margin: 0px; padding: 0px; color: rgb(133, 153, 0);">local</span><span style="margin: 0px; padding: 0px; color: rgb(101, 123, 131);">/</span><span class="hljs-keyword" style="margin: 0px; padding: 0px; color: rgb(133, 153, 0);">bin</span><span style="margin: 0px; padding: 0px; color: rgb(101, 123, 131);"> cocoapods</span></span></code>
4、更换repo镜像为国内服务器
所有的项目的Podspec文件都托管在https://github.com/CocoaPods/Specs。第一次执行pod setup时,CocoaPods会将这些podspec索引文件更新到本地的 ~/.cocoapods/目录下,这个索引文件比较大,有80M左右。所以第一次更新时非常慢,笔者(唐巧)就更新了将近1个小时才完成。一个叫akinliu的朋友在gitcafe和oschina上建立了CocoaPods索引库的镜像,因为gitcafe和oschina都是国内的服务器,所以在执行索引更新操作时,会快很多。如下操作可以将CocoaPods设置成使用gitcafe镜像:
</pre><pre class="hljs objectivec" name="code" style="white-space: pre-wrap; word-wrap: normal; padding: 9.5px; color: rgb(101, 123, 131); font-family: Menlo, Monaco, Consolas, 'Courier New', monospace; font-size: 13px; margin-top: 0px; margin-bottom: 20px; line-height: 20px; word-break: break-all; overflow: auto; background: rgb(253, 246, 227);"><code class="objectivec" style="padding: 0px; font-family: Menlo, Monaco, Consolas, 'Courier New', monospace; font-size: 12px; border: none; background-color: transparent;"><span class="hljs-comment" style="margin: 0px; padding: 0px; color: rgb(147, 161, 161);">// 1.移除原有服务器</span> - pod repo remove master <span class="hljs-comment" style="margin: 0px; padding: 0px; color: rgb(147, 161, 161);">// 2.添加境内服务器(建议使用第一个)</span> - pod repo add master https:<span class="hljs-comment" style="margin: 0px; padding: 0px; color: rgb(147, 161, 161);">//gitcafe.com/akuandev/Specs.git</span> - pod repo add master http:<span class="hljs-comment" style="margin: 0px; padding: 0px; color: rgb(147, 161, 161);">//git.oschina.net/akuandev/Specs.git(开源中国较慢)</span></code>
1、新建 Podfile 文件
使用时需要在你的项目根目录下新建一个名为Podfile的文件,将依赖的库名字依次列在文件中即可
<pre class="hljs objectivec" name="code" style="white-space: pre-wrap; word-wrap: normal; padding: 9.5px; color: rgb(101, 123, 131); font-family: Menlo, Monaco, Consolas, 'Courier New', monospace; font-size: 13px; margin-top: 0px; margin-bottom: 20px; line-height: 20px; word-break: break-all; overflow: auto; background: rgb(253, 246, 227);"><code class="objectivec" style="padding: 0px; font-family: Menlo, Monaco, Consolas, 'Courier New', monospace; font-size: 12px; border: none; background-color: transparent;"><span class="hljs-comment" style="margin: 0px; padding: 0px; color: rgb(147, 161, 161);">//进入你的工程目录</span> $ cd /Users/peikua/Desktop/CocoaPodsDemo <span class="hljs-comment" style="margin: 0px; padding: 0px; color: rgb(147, 161, 161);">//创建Pods文件</span> $ touch Podfile</code>
2、编辑 Podfile 文件
假设我们想导入 AFNetworking ,进入http://cocoapods.org,搜索AFNetworking
3、执行导入命令
<pre class="hljs ruby" name="code" style="white-space: pre-wrap; word-wrap: normal; padding: 9.5px; color: rgb(101, 123, 131); font-family: Menlo, Monaco, Consolas, 'Courier New', monospace; font-size: 13px; margin-top: 0px; margin-bottom: 20px; line-height: 20px; word-break: break-all; overflow: auto; background: rgb(253, 246, 227);"><code class="ruby" style="padding: 0px; font-family: Menlo, Monaco, Consolas, 'Courier New', monospace; font-size: 12px; border: none; background-color: transparent;"><span class="hljs-variable" style="margin: 0px; padding: 0px; color: rgb(181, 137, 0);">$ </span>cd /<span class="hljs-constant" style="margin: 0px; padding: 0px; color: rgb(181, 137, 0);">Users</span>/peikua/<span class="hljs-constant" style="margin: 0px; padding: 0px; color: rgb(181, 137, 0);">Desktop</span>/<span class="hljs-constant" style="margin: 0px; padding: 0px; color: rgb(181, 137, 0);">CocoaPodsDemo</span>
<span class="hljs-variable" style="margin: 0px; padding: 0px; color: rgb(181, 137, 0);">$ </span>pod install</code>
pod install命令执行成功后,会看到工程目录下多出CocoaPodsTest.xcworkspace、Podfile.lock文件和Pods目录。再看看刚才执行完pod install命令打印出来的内容的最后一行:
From now on use CocoaPodsDemo.xcworkspace.
提示我们从现在起,我们需要使用CocoaPodsTest.xcworkspace文件来开发。
4、第三方库更新
只需要在Podfile文件中添加相应的第三方库信息,执行pod update命令即可。
三、遭遇到的问题
<pre class="hljs markdown" name="code" style="white-space: pre-wrap; word-wrap: normal; padding: 9.5px; color: rgb(101, 123, 131); font-family: Menlo, Monaco, Consolas, 'Courier New', monospace; font-size: 13px; margin-top: 0px; margin-bottom: 20px; line-height: 20px; word-break: break-all; overflow: auto; background: rgb(253, 246, 227);"><code class="markdown" style="padding: 0px; font-family: Menlo, Monaco, Consolas, 'Courier New', monospace; font-size: 12px; border: none; background-color: transparent;">//以后使用CocoaPods过程中出现了莫名其妙的问题,执行下面命令 <span class="hljs-bullet" style="margin: 0px; padding: 0px;">- </span>sudo gem update --system <span class="hljs-bullet" style="margin: 0px; padding: 0px;">- </span>sudo gem install cocoapods <span class="hljs-bullet" style="margin: 0px; padding: 0px;">- </span>pod setup</code>
四、原理和说明
1、第三方库会被编译成.a静态库供我们真正的工程使用。
CocoaPods会将所有的第三方库以target的方式组成一个名为Pods的工程,该工程就放在刚才新生成的Pods目录下。整个第三方库工程会生成一个名称为libPods.a的静态库提供给我们自己的CocoaPodsTest工程使用。对于资源文件,CocoaPods提供了一个名为Pods-resources.sh的bash脚本,该脚本在每次项目编译的时候都会执行,将第三方库的各种资源文件复制到目标目录中。
2、我们的工程和第三方库所在的工程会由一个新生成的workspace管理
为了方便我们直观的管理工程和第三方库,CocoaPodsTest工程和Pods工程会被以workspace的形式组织和管理,也就是我们刚才看到的CocoaPodsTest.xcworkspace文件。
3、原来的工程设置已经被更改了,这时候我们直接打开原来的工程文件去编译就会报错,只能使用新生成的workspace来进行项目管理。
4、CocoaPods通过一个名为Pods.xcconfig的文件来在编译时设置所有的依赖和参数。
五、更多
1、pod install提速
每次执行pod install
和pod update
的时候,cocoapods都会默认更新一次spec仓库。这是一个比较耗时的操作。在确认spec版本库不需要更新时,给这两个命令加一个参数跳过spec版本库更新,可以明显提高这两个命令的执行速度。
<pre class="hljs sql" name="code" style="white-space: pre-wrap; word-wrap: normal; padding: 9.5px; color: rgb(101, 123, 131); font-family: Menlo, Monaco, Consolas, 'Courier New', monospace; font-size: 13px; margin-top: 0px; margin-bottom: 20px; line-height: 20px; word-break: break-all; overflow: auto; background: rgb(253, 246, 227);"><code class="sql" style="padding: 0px; font-family: Menlo, Monaco, Consolas, 'Courier New', monospace; font-size: 12px; border: none; background-color: transparent;">pod <span class="hljs-operator" style="margin: 0px; padding: 0px;"><span class="hljs-keyword" style="margin: 0px; padding: 0px; color: rgb(133, 153, 0);">install</span> <span class="hljs-comment" style="margin: 0px; padding: 0px; color: rgb(147, 161, 161);">--verbose --no-repo-update</span>
pod <span class="hljs-keyword" style="margin: 0px; padding: 0px; color: rgb(133, 153, 0);">update</span> <span class="hljs-comment" style="margin: 0px; padding: 0px; color: rgb(147, 161, 161);">--verbose --no-repo-update</span></span></code>
2、关于Podfile文件编辑时,第三方库版本号的各种写法
<pre class="hljs cpp" name="code" style="white-space: pre-wrap; word-wrap: normal; padding: 9.5px; color: rgb(101, 123, 131); font-family: Menlo, Monaco, Consolas, 'Courier New', monospace; font-size: 13px; margin-top: 0px; margin-bottom: 20px; line-height: 20px; word-break: break-all; overflow: auto; background: rgb(253, 246, 227);"><code class="cpp" style="padding: 0px; font-family: Menlo, Monaco, Consolas, 'Courier New', monospace; font-size: 12px; border: none; background-color: transparent;">pod ‘AFNetworking’ <span class="hljs-comment" style="margin: 0px; padding: 0px; color: rgb(147, 161, 161);">//不显式指定依赖库版本,表示每次都获取最新版本</span>
pod ‘AFNetworking’, ‘<span class="hljs-number" style="margin: 0px; padding: 0px; color: rgb(42, 161, 152);">2.0</span>’ <span class="hljs-comment" style="margin: 0px; padding: 0px; color: rgb(147, 161, 161);">//只使用2.0版本</span>
pod ‘AFNetworking’, ‘><span class="hljs-number" style="margin: 0px; padding: 0px; color: rgb(42, 161, 152);">2.0</span>′ <span class="hljs-comment" style="margin: 0px; padding: 0px; color: rgb(147, 161, 161);">//使用高于2.0的版本</span>
pod ‘AFNetworking’, ‘>=<span class="hljs-number" style="margin: 0px; padding: 0px; color: rgb(42, 161, 152);">2.0</span>′ <span class="hljs-comment" style="margin: 0px; padding: 0px; color: rgb(147, 161, 161);">//使用大于或等于2.0的版本</span>
pod ‘AFNetworking’, ‘<<span class="hljs-number" style="margin: 0px; padding: 0px; color: rgb(42, 161, 152);">2.0</span>′ <span class="hljs-comment" style="margin: 0px; padding: 0px; color: rgb(147, 161, 161);">//使用小于2.0的版本</span>
pod ‘AFNetworking’, ‘<=<span class="hljs-number" style="margin: 0px; padding: 0px; color: rgb(42, 161, 152);">2.0</span>′ <span class="hljs-comment" style="margin: 0px; padding: 0px; color: rgb(147, 161, 161);">//使用小于或等于2.0的版本</span>
pod ‘AFNetworking’, ‘~><span class="hljs-number" style="margin: 0px; padding: 0px; color: rgb(42, 161, 152);">0.1</span><span class="hljs-number" style="margin: 0px; padding: 0px; color: rgb(42, 161, 152);">.2</span>′ <span class="hljs-comment" style="margin: 0px; padding: 0px; color: rgb(147, 161, 161);">//使用大于等于0.1.2但小于0.2的版本,相当于>=0.1.2并且<0.2.0</span>
pod ‘AFNetworking’, ‘~><span class="hljs-number" style="margin: 0px; padding: 0px; color: rgb(42, 161, 152);">0.1</span>′ <span class="hljs-comment" style="margin: 0px; padding: 0px; color: rgb(147, 161, 161);">//使用大于等于0.1但小于1.0的版本</span>
pod ‘AFNetworking’, ‘~><span class="hljs-number" style="margin: 0px; padding: 0px; color: rgb(42, 161, 152);">0</span>′ <span class="hljs-comment" style="margin: 0px; padding: 0px; color: rgb(147, 161, 161);">//高于0的版本,写这个限制和什么都不写是一个效果,都表示使用最新版本</span></code>