文章記錄了Cocoapods使用中的一些注意事項,所需資料均來源互聯(lián)網(wǎng)和官方文檔愧旦,如有不明確的地方歡迎指出跌造。Cocoapods官網(wǎng)
以下只是一部分,后續(xù)會繼續(xù)更新间聊,敬請關(guān)注~
Podfile
podfile是一個規(guī)范攒盈,它描述了一個或多個Xcode工程文件的目標(biāo)文件(targets)所使用到的各種依賴關(guān)系。所以在使用之前我們需要生成一個podfile文件:
$ pod init
然后可以將使用到的依賴庫的pod信息添加到依賴文件中去哎榴,這里以添加Alamofire依賴庫到工程目標(biāo)文件中為例:
target 'MyApp' do
use_frameworks!
pod 'Alamofire','~>3.0'
end
注:Alamofire是swift版本的AFNetworking
高級使用
下面例子展示了如何將一個podfile關(guān)聯(lián)到一個app和它的測試用例包中型豁,以及如何同時引用官方spec依賴庫和私有spec依賴庫:
source 'https://github.com/CocoaPods/Specs.git'
source 'https://github.com/Artsy/Specs.git'
platform :ios, '9.0'
inhibit_all_warnings!
target 'MyApp' do
pod 'GoogleAnalytics', '~> 3.1'
#Has its own copy of OCMock
#and has access to GoogleAnalytics via the app
#that hosts the test target
target 'MyAppTests' do
inherit! :search_paths
pod 'OCMock', '~> 2.0.1'
end
end
post_install do |installer|
installer.pods_project.targets.each do |target|
puts target.name
end
end
注:多個目標(biāo)文件共享一個pods,使用abstract_target,具體參考Cocoapods官網(wǎng)
熟悉指定 pod versions(依賴庫版本)的一些規(guī)則尚蝌,能幫助我們更好地引入和管理我們需要的依賴庫
- 使用默認(rèn)的最新版本
pod 'SSZipArchive'
- 使用指定版本號的依賴庫
pod 'Objection','0.9'
- 使用邏輯運(yùn)算符指定版本號
pod 'AFNetworking','> 2.0'//任何大于2.0的版本
pod 'ASINetworking','>= 0.1'//任何大于等于0.1的版本 - 常用邏輯運(yùn)算符
'> 0.1' 任何高于0.1的版本
'>= 0.1' 任何高于0.1的版本迎变,包含0.1版本
'< 0.1' 任何低于0.1的版本
'<= 0.1' 任何低于0.1的版本,包含0.1版本
'~> 0.1.2' 任何0.1.2到0.2的版本,包含0.1.2但不包含0.2飘言,即0.1.2 =< version < 0.2
'~> 0.1' 0.1版本和任何介于0.1-1.0之間的版本衣形,不包含1.0
'~> 0' 任何版本
更多使用請閱讀:
cocoapods進(jìn)階
cocoapods更新篇