一、Podfile.lock文件
该文件用于保存已经安装的Pods依赖库的版本,通过CocoaPods安装了SBJson、AFNetworking、Reachability三个POds依赖库以后对应的Podfile.lock文件内容为:
PODS: - AFNetworking (2.3.1): - AFNetworking/NSURLConnection - AFNetworking/NSURLSession - AFNetworking/Reachability - AFNetworking/Security - AFNetworking/Serialization - AFNetworking/UIKit - AFNetworking/NSURLConnection (2.3.1): - AFNetworking/Reachability - AFNetworking/Security - AFNetworking/Serialization - AFNetworking/NSURLSession (2.3.1): - AFNetworking/Reachability - AFNetworking/Security - AFNetworking/Serialization - AFNetworking/Reachability (2.3.1) - AFNetworking/Security (2.3.1) - AFNetworking/Serialization (2.3.1) - AFNetworking/UIKit (2.3.1): - AFNetworking/NSURLConnection - AFNetworking/NSURLSession - FXReachability (1.1.1) - GCJSONKit (1.5.0) DEPENDENCIES: - AFNetworking (~> 2.3.1) - FXReachability (~> 1.1.1) - GCJSONKit (~> 1.5.0) SPEC CHECKSUMS: AFNetworking: 6d7b76aa5d04c8c37daad3eef4b7e3f2a7620da3 FXReachability: 34c68085c9a12c7fe4f791bdb54e99f14d90b5ae GCJSONKit: 2e66dba7183b1557f01bcbb3021746073c6fa523 COCOAPODS: 0.33.1
Podfile.lock文件最大得用处在于多人开发。对于没有在Podfile中指定Pods依赖库版本的写法,如下:
pod JSONKIT
该句话用于获取当前SBJson这个Pods依赖库的最新版本。
当团队中的某个人执行完pod install命令后,生成的Podfile.lock文件就记录下了当时最新Pods依赖库的版本,这时团队中的其它人check下来这份包含Podfile.lock文件的工程以后,再去执行pod install命令时,获取下来的Pods依赖库的版本就和最开始用户获取到的版本一致。如果没有Podfile.lock文件,后续所有用户执行pod install命令都会获取最新版本的SBJson,这就有可能造成同一个团队使用的依赖库版本不一致,这对团队协作来说绝对是个灾难!
- 更改Podfile,使其指向最新版本的JSONKIT依赖库;
- 执行pod update命令;
二、Podfile文件
1、Podfile文件存放位置
这是在上篇文章中,遗留的一个问题。通常情况下我们都推荐Podfile文件都放在工程根目录,如下图所示:
事实上Podfile文件可以放在任意一个目录下,需要做的是在Podfile中指定工程的路径,和原来相比,Podfile文件就在最开始的位置增加了一行,具体内容如下:
xcodeproj "/Users/admin/Documents/CocoaPodsFirst/CocoaPodsFirst.xcodeproj"
platform :ios
pod 'FXReachability', '~> 1.1.1'
pod 'GCJSONKit', '~> 1.5.0'
platform :ios ,'7.0'
pod 'AFNetworking', '~> 2.3.1'
指定路径使用的是xcodeproj关键字。此后,进入Podfile文件所在路径,执行pod install命令就会和之前一样下载这些Pods依赖库,而且生成的相关文件都放在了Podfile所在目录下面,如下图:
和之前一样,我们仍然需要使用这里生成的workspace文件打开工程。
2、Podfile和target
比如,名称为CocoaPodsTest的target和Second的target都需要使用Reachability、SBJson、AFNetworking三个Pods依赖库,可以使用link_with关键字来实现,将Podfile写成如下方式:
link_with 'CocoaPodsFirstExample','CocoaPodsSecondExample'
platform :ios
pod 'FXReachability', '~> 1.1.1'
pod 'GCJSONKit', '~> 1.5.0'
platform :ios ,'7.0'
pod 'AFNetworking', '~> 2.3.1'
这种写法就实现了CocoaPodsFirstExample和CocoaPodsSecondExample两个target共用相同的Pods依赖库。
CocoaPodsFirstExample这个target使用的是Reachability、SBJson、AFNetworking三个依赖库,但CocoaPodsSecondExample这个target只需要使用OpenUDID这一个依赖库,这时可以使用target关键字,Podfile的描述方式如下:(注意target后面的:不能有空格)
target :'CocoaPodsFirstExample' do
platform :ios
pod 'FXReachability', '~> 1.1.1'
pod 'GCJSONKit', '~> 1.5.0'
platform :ios ,'7.0'
pod 'AFNetworking', '~> 2.3.1'
end
target :'CocoaPodsSecondExample' do
platform :ios
pod 'OpenUDID', '~> 1.0.0'
end
其中,do/end作为开始和结束标识符。
3、使用Podfile管理Pods依赖库版本
pod 'AFNetworking' //不显式指定依赖库版本,表示每次都获取最新版本 pod 'AFNetworking', '2.0' //只使用2.0版本 pod 'AFNetworking', '> 2.0' //使用高于2.0的版本 pod 'AFNetworking', '>= 2.0' //使用大于或等于2.0的版本 pod 'AFNetworking', '< 2.0' //使用小于2.0的版本 pod 'AFNetworking', '<= 2.0' //使用小于或等于2.0的版本 pod 'AFNetworking', '~> 0.1.2' //使用大于等于0.1.2但小于0.2的版本 pod 'AFNetworking', '~>0.1' //使用大于等于0.1但小于1.0的版本 pod 'AFNetworking', '~>0' //高于0的版本,写这个限制和什么都不写是一个效果,都表示使用最新版本
三、CocoaPods常用命令
1、pod install
2、pod update
3、pod search
后面的OpenUDID为参数。从命令的名称不难看出,该命令是用来按名称搜索可用的Pods依赖库,执行结果如下:
-> OpenUDID (1.0.0) Open source initiative for a universal and persistent UDID solution for iOS. pod 'OpenUDID', '~> 1.0.0' - Homepage: http://OpenUDID.org - Source: https://github.com/ylechelle/OpenUDID.git - Versions: 1.0.0 [master repo]
pod 'OpenUDID', '~> 1.0.0'
不难看出,这是我们需要添加到Podfile文件中的。
4、pod setup
命令格式为:$ pod setup
执行完了以后会打印:
Setting up CocoaPods master repo Updating 7cd4668..f3d3ced Fast-forward
接下来还会打印很多更新信息。这条命令用于跟新本地电脑上的保存的Pods依赖库tree。由于每天有很多人会创建或者更新Pods依赖库,这条命令执行的时候会相当慢,还请耐心等待。我们需要经常执行这条命令,否则有新的Pods依赖库的时候执行pod search命令是搜不出来的。
转载自:http://blog.csdn.net/wzzvictory/article/details/19178709
参照文档:http://guides.cocoapods.org/using/index.html