每种语言发展到一个阶段,就会出现相应的依赖管理工具, 或者是中央代码仓库。好比ios
随着iOS开发者的增多,业界也出现了为iOS程序提供依赖管理的工具,这个工具叫:CocoaPods。git
CocoaPods是一个负责管理iOS项目中第三方开源代码的工具。CocoaPods项目的源码在Github上管理。该项目开始于2011年8月12日,通过一年多的发展,如今已经超过1000次提交,而且持续保持活跃更新。开发iOS项目不可避免地要使用第三方开源库,CocoaPods的出现使得咱们能够节省设置和更新第三方开源库的时间。github
拿我以前开发的粉笔网iPhone客户端为例,其使用了14个第三方开源库。在没有使用CocoaPods之前,我须要:正则表达式
这些体力活虽然简单,但毫无技术含量而且浪费时间。在使用CocoaPods以后,我只须要将用到的第三方开源库放到一个名为Podfile的文件中,而后执行pod install。CocoaPods就会自动将这些第三方开源库的源码下载下来,而且为个人工程设置好相应的系统依赖和编译参数。npm
安装方式异常简单, Mac下都自带ruby,使用ruby的gem命令便可下载安装:json
1 2 |
$ sudo gem install cocoapods $ pod setup |
上面第二行执行时,会输出Setting up CocoaPods master repo
,可是会等待比较久的时间。这步实际上是Cocoapods在将它的信息下载到 ~/.cocoapods
目录下,若是你等过久,能够试着cd到那个目录,用du -sh *
来查看下载进度。xcode
若是你的gem太老,可能也会有问题,能够尝试用以下命令升级gem:ruby
1
|
sudo gem update --system |
另外,ruby的软件源rubygems.org由于使用的亚马逊的云服务,因此被墙了,须要更新一下ruby的源:bash
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管理的库中,是否有你想要的库,那么你能够经过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] |
当你执行pod install
以后,除了Podfile外,cocoapods还会生成一个名为Podfile.lock
的文件,你不该该把这个文件加入到.gitignore
中。由于Podfile.lock
会锁定当前各依赖库的版本,以后若是屡次执行pod install
不会更改版本,要pod update
才会改Podfile.lock
了。这样多人协做的时候,能够防止第三方库升级把程序搞挂。
若是你想让CococaPods帮你生成第三方库的帮助文档,并集成到XCode中,那么用brew安装appledoc便可:
1
|
brew install appledoc |
关于appledoc,我在今年初的另外一篇博客《使用Objective-C的文档生成工具:appledoc》中有专门介绍。它最大的优势是能够将帮助文档集成到XCode中,这样你在敲代码的时候,按住opt键单击类名或方法名,就能够显示出相应的帮助文档。
大概研究了一下CocoaPods的原理,它是将全部的依赖库都放到另外一个名为Pods项目中,而后让主项目依赖Pods项目,这样,源码管理工做都从主项目移到了Pods项目中。发现的一些技术细节有:
Have fun!