CocoaPods的安装和使用介绍
安装
安装方式异常简单, Mac下都自带ruby,使用ruby的gem命令即可下载安装:
1
2
| $ sudo gem install cocoapods
$ pod setup
|
上面第二行执行时,会输出Setting up CocoaPods master repo
,但是会等待比较久的时间。这步其实是Cocoapods在将它的信息下载到 ~/.cocoapods
目录下,如果你等太久,可以试着cd到那个目录,用du -sh *
来查看下载进度。
如果你的gem太老,可能也会有问题,可以尝试用如下命令升级gem:
1
| sudo gem update --system
|
另外,ruby的软件源rubygems.org因为使用的亚马逊的云服务,所以被墙了,需要更新一下ruby的源:
1
2
3
| gem sources --remove https://rubygems.org/
gem sources -a http://ruby.taobao.org/
gem sources -l
|
使用
使用时需要新建一个名为Podfile的文件,以如下格式,将依赖的库名字依次列在文件中即可
1
2
3
4
5
| platform :ios
pod 'JSONKit', '~> 1.4'
pod 'Reachability', '~> 3.0.0'
pod 'ASIHTTPRequest'
pod 'RegexKitLite'
|
然后你将编辑好的Podfile文件放到你的项目根目录中,执行如下命令即可:
1
2
| cd "your project home"
pod install
|
现在,你的所有第三方库都已经下载完成并且设置好了编译参数和依赖,你只需要记住如下2点即可:
- 使用CocoaPods生成的 .xcworkspace 文件来打开工程,而不是以前的 .xcodeproj 文件。
- 每次更改了Podfile文件,你需要重新执行一次pod install命令。
查找第三方库
你如果不知道cocoaPods管理的库中,是否有你想要的库,那么你可以通过pod search命令进行查找,以下是我用pod search json查找到的所有可用的库:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
| $ pod search json
-> AnyJSON (0.0.1)
Encode / Decode JSON by any means possible.
- Homepage: https://github.com/mattt/AnyJSON
- Source: https://github.com/mattt/AnyJSON.git
- Versions: 0.0.1 [master repo]
-> JSONKit (1.5pre)
A Very High Performance Objective-C JSON Library.
- Homepage: https://github.com/johnezang/JSONKit
- Source: git://github.com/johnezang/JSONKit.git
- Versions: 1.5pre, 1.4 [master repo]
-> MTJSONDictionary (0.0.4)
An NSDictionary category for when you're working with it converting to/from JSON. DEPRECATED, use MTJSONUtils
instead.
- Homepage: https://github.com/mysterioustrousers/MTJSONDictionary.git
- Source: https://github.com/mysterioustrousers/MTJSONDictionary.git
- Versions: 0.0.4, 0.0.3, 0.0.2 [master repo]
-> MTJSONUtils (0.1.0)
An NSObject category for working with JSON.
- Homepage: https://github.com/mysterioustrousers/MTJSONUtils.git
- Source: https://github.com/mysterioustrousers/MTJSONUtils.git
- Versions: 0.1.0, 0.0.1 [master repo]
-> SBJson (3.1.1)
This library implements strict JSON parsing and generation in Objective-C.
- Homepage: http://stig.github.com/json-framework/
- Source: https://github.com/stig/json-framework.git
- Versions: 3.1.1, 3.1, 3.0.4, 2.2.3 [master repo]
-> TouchJSON (1.0)
TouchJSON is an Objective-C based parser and generator for JSON encoded data.
- Homepage: https://github.com/TouchCode/TouchJSON
- Source: https://github.com/TouchCode/TouchJSON.git
- Versions: 1.0 [master repo]
|
关于.gitignore
当你执行pod install
之后,除了Podfile外,cocoapods还会生成一个名为Podfile.lock
的文件,你不应该把这个文件加入到.gitignore
中。因为Podfile.lock
会锁定当前各依赖库的版本,之后如果多次执行pod install
不会更改版本,要pod update
才会改Podfile.lock
了。这样多人协作的时候,可以防止第三方库升级把程序搞挂。
生成第三方库的帮助文档
如果你想让CococaPods帮你生成第三方库的帮助文档,并集成到XCode中,那么用brew安装appledoc即可:
它最大的优点是可以将帮助文档集成到XCode中,这样你在敲代码的时候,按住opt键单击类名或方法名,就可以显示出相应的帮助文档。
原理
大概研究了一下CocoaPods的原理,它是将所有的依赖库都放到另一个名为Pods项目中,然后让主项目依赖Pods项目,这样,源码管理工作都从主项目移到了Pods项目中。发现的一些技术细节有:
- Pods项目最终会编译成一个名为libPods.a的文件,主项目只需要依赖这个.a文件即可。
- 对于资源文件,CocoaPods提供了一个名为Pods-resources.sh的bash脚本,该脚本在每次项目编译的时候都会执行,将第三方库的各种资源文件复制到目标目录中。
- CocoaPods通过一个名为Pods.xcconfig的文件来在编译时设置所有的依赖和参数。