CocoaPods

CocoaPods一个Objective-C第三方库的管理利器。类似于Maven的。

安装

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
 $ sudo gem install cocoapods
 Password: 输入密码后就开始安装了,没有任何输出,需要等几分钟...
 Building native extensions. This could take a while...
 Building native extensions. This could take a while...
 Successfully installed xcodeproj-0.5.5
 Successfully installed escape-0.0.4
 Successfully installed json-1.7.7
 Successfully installed open4-1.3.0
 Successfully installed cocoapods-0.18.1
 5 gems installed
 Installing ri documentation for xcodeproj-0.5.5...
 Installing ri documentation for escape-0.0.4...
 Installing ri documentation for json-1.7.7...
 Installing ri documentation for open4-1.3.0...
 Installing ri documentation for cocoapods-0.18.1...
 Installing RDoc documentation for xcodeproj-0.5.5...
 Installing RDoc documentation for escape-0.0.4...
 Installing RDoc documentation for json-1.7.7...
 Installing RDoc documentation for open4-1.3.0...
 Installing RDoc documentation for cocoapods-0.18.1...

搜索

安装好后,就可以使用search命令来搜索你想要的library,例如:

1
2
3
4
5
6
7
$ pod search JSONKit

-> JSONKit (1.5pre)
   A Very High Performance Objective-C JSON Library.
   - Homepage: https://github.com/johnezang/JSONKit
   - Source:   https://github.com/johnezang/JSONKit.git
   - Versions: 1.5pre, 1.4 [master repo]

应用

以下通过一个实例演示,如何在一个xcode项目中使用pod

首先创建一个xcode项目,名字叫CocoaPodsTest,然后cd到工程所在目录ls一下

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
$ ls
CocoaPodsTest		CocoaPodsTest.xcodeproj
$ pod install
[!] No `Podfile' found in the current working directory.
$ sudo touch Podfile
Password:
$ vi Podfile

添加以下内容,保存退出
platform :ios
pod 'JSONKit'
$ pod install
Analyzing dependencies
Downloading dependencies
Installing JSONKit (1.5pre)
Generating Pods project
Integrating client project

[!] From now on use `CocoaPodsTest.xcworkspace`.

$ ls
CocoaPodsTest			CocoaPodsTest.xcworkspace	Podfile.lock
CocoaPodsTest.xcodeproj		Podfile				Pods
$ pod update
Analyzing dependencies
Downloading dependencies
Using JSONKit (1.5pre)
Generating Pods project
Integrating client project

现在通过双击CocoaPodsTest.xcworkspace打开项目就可以了;)

屏幕快照 2013-04-26 下午10.16.13
如果在使用import找不到头文件,需要再target里设置User Header Search Path为${SRCROOT},同时选择recursive。