CocoaPods是用ruby實現的,所以Podfile文件的語法就是ruby的語法。
podfile是一個說明文件,用以描述管理一個或者多個Xcode project的target的依賴庫。這個文件應該且必須被命名爲Podfile
。
Podfile能夠很是簡單,下面的例子增長了Alamofire依賴庫到單個target:html
target 'MyApp' do use_frameworks! pod 'Alamofire', '~> 3.0' end
下面是一個更復雜的例子,Podfile連接了app和它的測試bundle: ios
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
若是你但願多個target共享同一個pods,那麼能夠用關鍵字abstract_target
:git
# There are no targets called "Shows" in any Xcode projects abstract_target 'Shows' do pod 'ShowsKit' pod 'Fabric' # Has its own copy of ShowsKit + ShowWebAuth target 'ShowsiOS' do pod 'ShowWebAuth' end # Has its own copy of ShowsKit + ShowTVAuth target 'ShowsTV' do pod 'ShowTVAuth' end end
Podfile中自帶一個隱藏的、默認的abstract target
,因此你也能夠用以下的方式達到上面例子的一樣效果:github
pod 'ShowsKit' pod 'Fabric' # Has its own copy of ShowsKit + ShowWebAuth target 'ShowsiOS' do pod 'ShowWebAuth' end # Has its own copy of ShowsKit + ShowTVAuth target 'ShowsTV' do pod 'ShowTVAuth' end
當開始一個項目,你可能會想要使用最新版本的pod依賴庫。 若是是這種狀況,只需忽略版本要求。 swift
pod 'SSZipArchive'
稍後在項目您可能想要使用特定版本的pod依賴庫,在這種狀況下,您能夠指定版本號。xcode
pod 'Objection', '0.9'
除了沒有版本,或特定的一個,也可使用邏輯運算符: ruby
除了邏輯運算符,還有一種運算符:bash
若是你想創建一個本地依賴庫和項目之間的關係,即項目依賴本地文件夾的某個依賴庫,能夠用關鍵字path
:app
pod 'Alamofire', :path => '~/Documents/Alamofire'ide
使用倉庫的master分支:
pod 'Alamofire', :git => 'https://github.com/Alamofire/Alamofire.git'
使用倉庫中其餘的分支:
pod 'Alamofire', :git => 'https://github.com/Alamofire/Alamofire.git'
使用指定tag的分支:
pod 'Alamofire', :git => 'https://github.com/Alamofire/Alamofire.git', :tag => '3.1.1'
或者使用指定commit號的版本:
pod 'Alamofire', :git => 'https://github.com/Alamofire/Alamofire.git', :commit => '0f506b1c45'
使用path
將把本地文件夾做爲pod依賴庫的源,而且將會直接從給定的文件夾中把pod依賴庫連接進pod項目。這意味着咱們對這個本地文件夾的編輯與修改將會被pod直接更新。
source 'URL' : 指定鏡像倉庫的源
platform : ios, '6.0' : 指定所支持系統和最低版本
inhibit_all_warnings! :屏蔽全部warning
workspace '項目空間名': 指定項目空間名
xcodeproj '工程文件名':指定xcodeproj工程文件名
~ > : 從指定版本到倒數第二位版本號升1爲止,好比 '~> 1.2.1'是指 1.2.1 <= 版本 < 1.3.0。'~>1.2'是指1.2<= 版本 < 2.0
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的版本,寫這個限制和什麼都不寫是一個效果,都表示使用最新版本
/默認執行pod install 或者 pod update命令,會更新遠程cocopodsde spec庫。因此致使命令執行的比較慢,咱們能夠採用如下命令來提升更新速度,不更新CocoaPods的spec倉庫直接install/update。
pod update --verbose --no-repo-update
pod install --verbose --no-repo-update
Podfile本質上是用來描述Xcode工程中的targets用的。若是咱們不顯式指定Podfile對應的target,CocoaPods會建立一個名稱爲default的隱式target,會和咱們工程中的第一個target相對應。換句話說,若是在Podfile中沒有指定target,那麼只有工程裏的第一個target可以使用Podfile中描述的Pods依賴庫。
項目存在多個Target的時候,須要配置Podfile文件來支持新增長的Target,不然只支持項目默認創建時生成的Target:
a、默認新建的項目只有一個target,可是咱們能夠給項目天阿基額外的target。若是項目中存在多個target,且兩個Target所依賴的的第三方庫徹底相同,則可使用link_with
關鍵字,將Podfile寫成以下方式:
link_with 'CocoaPodsTest', 'Second' platform :ios platform :ios, ‘9.0’ pod 'AFNetworking', '~> 2.0'
或
link_with 'CocoaPodsTest', 'Second' platform :ios pod 'Reachability', '~> 3.0.0' pod 'SBJson', '~> 4.0.0' platform :ios, '7.0' pod 'AFNetworking', '~> 2.0'
若是你按照上面的寫法編輯Podfile,而後執行pod undate 或pod install,你會發現如下的錯誤:
意思是link_with
關鍵字已通過期,如今不在支持,咱們須要用target blocks來代替link_with
。查看官方文檔,以下:
因此,咱們能夠經過abstract_target
和target繼承的方式代替link_with
.
若是不一樣的target使用不一樣的依賴庫,則能夠:
platform :ios target :'Test' do pod 'Reachability' pod 'SBJson' pod 'AFNetworking' end target :'Second' do pod 'OpenUDID' end
用於屏蔽cocoapods庫裏面的全部警告。
這個特性也能在子target裏面定義,若是你想屏蔽某pod裏面的警告也是能夠的:
pod 'SSZipArchive', :inhibit_warnings => true
在pods中用frameworks替代靜態庫。
另,通常開發swift項目時,咱們會在podfile中添加這一句。
後綴名爲podspec(cocoapods specification)的文件是cocoapods的說明文件,該文件爲Pods依賴庫的描述文件,每一個Pods依賴庫必須有且僅有那麼一個描述文件。該文件包括依賴庫的名字、版本、描述、license、author、source、platform等信息。
其餘連接:
Podfile語法參考(譯)
Cocoapods的Podfile常見語法總結
CocoaPods詳解之----使用篇
CocoaPods詳解之----進階篇
CocoaPods詳解之----製做篇
CocoaPods安裝和使用教程以及一些常見問題
如何製做本身的Pod庫
The Podfile(英)
Podfile語法參考(譯)
(實驗)Podfile的target與link_with
Podfile Syntax Reference(英)
Podfile Syntax Reference(英)