一粘招、什么是CocoaPods
CocoaPods是iOS項目的依賴管理工具啥寇,該項目源碼在Github上管理。開發(fā)iOS項目不可避免地要使用第三方開源庫洒扎,CocoaPods的出現(xiàn)使得我們可以節(jié)省設(shè)置和第三方開源庫的時間辑甜。在使用CocoaPods之前,開發(fā)項目需要用到第三方開源庫的時候袍冷,我們需要1.把開源庫的源代碼復(fù)制到項目中2.添加一些依賴框架和動態(tài)庫3.設(shè)置-ObjC磷醋,-fno-objc-arc等參數(shù)4.管理他們的更新在使用CocoaPods后,我們只需要把用到的開源庫放到一個名為Podfile的文件中胡诗,然后執(zhí)行pod install.Cocoapods就會自動將這些第三方開源庫的源碼下載下來邓线,并且為我們的工程設(shè)置好響應(yīng)的系統(tǒng)依賴和編譯參數(shù)。
二煌恢、CocoaPods的原理
CocoaPods的原理是將所有的依賴庫都放到另一個名為Pods的項目中骇陈,然后讓主項目依賴Pods項目,這樣瑰抵,源碼管理工作都從主項目移到了Pods項目中你雌。Pods項目最終會編譯成一個名為libPods.a的文件,主項目只需要依賴這個.a文件即可二汛。
三匪蝙、CocoaPods的安裝與更新
CocoaPods可以通過Mac自帶的RubyGems進(jìn)行安裝與更新。
打開Terminal习贫,然后鍵入以下命令:$ sudo gem install -n /usr/local/bin cocoapods
執(zhí)行完如果報以下錯誤:
ERROR: Could not find a valid gem 'cocoapods' (>= 0), here is why:
Unable to download data from https://rubygems.org/ - Errno::ETIMEDOUT: Operation timed out - connect(2)
ERROR: Possible alternatives: cocoa pods
是因為ruby的軟件源rubygems.org使用的亞馬遜云服務(wù),被我天朝屏蔽了千元,需要更新一下ruby的源苫昌,過程如下:
$ gem sources -l //查看當(dāng)前ruby的源
$ gem sources --remove https://rubygems.org/
$ gem sources -a https://gems.ruby-china.org
$ gem sources -l
接下來用$ sudo gem update --system
命令升級gem。若報錯
Operation not permitted - /usr/bin/update_rubygems
嘗試命令$ brew install ruby
直到出現(xiàn)?? /usr/local/Cellar/ruby/2.2.3: 1080 files, 20M, built in 3.3 minutes
后
再次輸入$ sudo gem update --system
升級gem幸海,成功后提示:
RubyGems system software updated
然后重新執(zhí)行命令 $ sudo gem install -n /usr/local/bin cocoapods
如果初次安裝祟身,還需執(zhí)行:$ pod setup
把最新的類庫信息拉取到本地spec文件。Terminal會停留在 Setting up CocoaPods master repo 這個狀態(tài)一段時間物独,需要耐心等待袜硫。安裝成功后,你會看到:
Setup completed
四、Cocoapods的使用
查看pod版本
$ pod --version
-
查找第三方庫
$ pod search AFNetworking
按q即可退出挡篓。
如果搜索不到:
1婉陷、請先進(jìn)行:$ pod setup
2帚称、再清空一下搜索索引,讓pod重建索引:
$ rm ~/Library/Caches/CocoaPods/search_index.json
這是因為之前pod search的時候生成了緩存文件search_index.json -
創(chuàng)建Podfile文件
cd到工程根目錄下秽澳,輸入$ vim Podfile
創(chuàng)建文件闯睹。按i進(jìn)入插入模式,輸入文本担神,按esc進(jìn)入命令模式后楼吃,按:wq或ZZ退出并保存。
安裝第三方庫
$ pod install
1.成功后需要使用Cocoapods生成的 .xcworkspace文件來打開工程妄讯,而不是使用以前的.xcodeproj文件
2.執(zhí)行之后孩锡,除了Podile,還會生成一個名為Podfile.lock的文件亥贸,它會鎖定當(dāng)前各依賴庫的版本躬窜。之后即使多次執(zhí)行$ pod install
也不會更改版本,只有執(zhí)行pod update才會改變Podfile.lock砌函。在多人協(xié)作的時候斩披,這樣可以防止第三方庫升級時候造成大家各自的第三方庫版本不一致。所以在提交版本的時候不能把它落下讹俊,也不要添加到.gitignore中
3.每次更改了Podfile文件垦沉,都需要重新執(zhí)行一次$ pod update
命令卸載pod
1.$ which pod
查看pod所在路徑 比如 /usr/local/bin/pod
2.$ rm -rf /usr/local/bin/pod
3.$ gem list | grep cocoapods
cocoapods (1.0.1)
cocoapods-core (1.0.1, 0.39.0, 0.38.2, 0.38.1, 0.37.2)
cocoapods-deintegrate (1.0.0)
cocoapods-downloader (1.1.0, 0.9.3, 0.9.1)
cocoapods-plugins (1.0.0, 0.4.2)
cocoapods-search (1.0.0, 0.1.0)
cocoapods-stats (1.0.0, 0.6.2, 0.5.3)
cocoapods-trunk (1.0.0, 0.6.4, 0.6.1)
cocoapods-try (1.1.0, 0.5.1, 0.4.5)
4.$ sudo gem uninstall xxx
$ sudo gem uninstall cocoapods
$ sudo gem uninstall cocoapods-core
$ sudo gem uninstall cocoapods-deintegrate
$ sudo gem uninstall cocoapods-downloader
$ sudo gem uninstall cocoapods-plugins
$ sudo gem uninstall cocoapods-search
$ sudo gem uninstall cocoapods-stats
$ sudo gem uninstall cocoapods-trunk
$ sudo gem uninstall cocoapods-try
五、常見問題
[!] Invalid Podfile file: undefined local variable or method `en_US' for #<Pod::Podfile:0x00000102a5d8b0>. Updating CocoaPods might fix the issue.
原因:單引號格式仍劈,可能是手動輸入導(dǎo)致
解決辦法:系統(tǒng)偏好設(shè)置-鍵盤-文本-將“使用智能引號和破折號”一項取消勾選-再將podfile里面的單(雙)引號修改一下[!] The YMTea [Release] target overrides the OTHER_LDFLAGS build setting defined in `Pods/Target Support Files/Pods/Pods.release.xcconfig'. This can lead to problems with the CocoaPods installation
原因:項目 Target 中做了一些設(shè)置厕倍,CocoaPods 也做了默認(rèn)的設(shè)置,如果兩個設(shè)置結(jié)果不一致贩疙,就會造成問題
解決方法:想要使用 CocoaPods 中的設(shè)置讹弯,分別在項目中定義PODS_ROOT 和 Other Linker Flags的地方(build settings),把他們的值用$(inherited)替換掉这溅,進(jìn)入終端组民,執(zhí)行 $ pod update-
[!] Oh no, an error occurred.
It appears to have originated from your Podfile at line 2.
原因:Podfile文件里面 platform 那一行 冒號和ios之間多了一個空格
The dependency **** is not used in any concrete target.
原因:cocoapods升級為1.0以后,Podfile文件書寫格式的問題CocoaPods管理的第三方庫頭文件無法自動補(bǔ)齊問題
解決辦法:
Target -> Build Settings 悲靴,User Header Search Paths條目中臭胜,添加${SRCROOT}或者$(PODS_ROOT),并且選擇Recursive癞尚,遞歸搜索耸三,然后就可以自動補(bǔ)齊了。framework not found Pods_ChainsProject
解決辦法:
在終端cd到工程目錄下執(zhí)行以下命令
1 . pod deintegrate
2 . pod install
高級用法
指定源
CocoaPods支持私有 Spec 倉庫的浇揩,我們可以建立自己的源仪壮,也可以使用非官方的源,只要是符合規(guī)定的都可以指定胳徽。
source 'https://github.com/artsy/Specs.git'
source 'https://github.com/CocoaPods/Specs.git'
抑制警告
inhibit_warnings參數(shù)能夠有效的抑制CocoaPods引入的第三方代碼庫產(chǎn)生的warning积锅。
inhibit_all_warnings!
全部指定
pod 'ReactiveCocoa', '~> 2.4', :inhibit_warnings => true
針對指定爽彤。
pod 'ISO8601DateFormatter', :head
使用git的HEAD指向的分支
pod 'ARAnalytics/Mixpanel', :git => 'https://github.com/orta/ARAnalytics.git'
使用 master 分支
pod 'Reachability', :git => 'https://github.com/ashfurrow/Reachability.git', :branch => 'frameworks'
指定branch
pod 'AFNetworking', :git => 'https://github.com/gowalla/AFNetworking.git', :tag => '0.7.0'
指定tag
pod 'ARTiledImageView', :git => 'https://github.com/dblockARTiledImageView', :commit => '1a31b864d1d56b1aaed0816c10bb55cf2e078bb8'
指定commit使用子庫
pod 'QueryKit/Attribute'
pod 'QueryKit', :subspecs => ['Attribute', 'QuerySet']
指定多個子庫使用本地代碼
pod 'AFNetworking', :path => '~/Documents/AFNetworking'
通過:path可以指定本地代碼,不過需要確保目錄包含podspec文件乏沸。指定target的依賴庫
target :ZipApp do
pod 'SSZipArchive'
end
- 排除taget
target 'Artsy Tests', :exclusive => true do
pod 'FBSnapshotTestCase', '1.4'
end
- 指定連接的target
如果不顯式指定連接的target淫茵,Pods會默認(rèn)連接project的第一個target。如果需要蹬跃,可以使用link_with指定連接一個或多個target
link_with 'MyApp', 'MyOtherApp'
- 指定環(huán)境
如下只會在Debug環(huán)境下面加入PonyDebugger庫到工程匙瘪。
pod 'PonyDebugger', :configuration => ['Debug']
- 指定target的配置文件
xcodeproj 'TestProject', 'Mac App Store' => :release, 'Test' => :debug
使用Dynamic Frameworks代替Static Libraries
通過標(biāo)志use_frameworks!
可開啟這個功能。如果需要使用Swift的庫蝶缀,就必須加上這個標(biāo)志了丹喻。加快pod install/update 速度
使用CocoaPods來添加第三方類庫,無論是執(zhí)行pod install
還是pod update
很多時候都卡在了Analyzing dependencies不動翁都,這是更新本地的pod spec索引文件導(dǎo)致的碍论。通過--no-repo-update
標(biāo)志可以不更新本地pod spec索引。當(dāng)然首次install不應(yīng)該添加這個標(biāo)志柄慰,后續(xù)修改Podfile的時候可以適當(dāng)使用鳍悠,加快pod速度。
pod install --no-repo-update
pod update --no-repo-update
- 輸出詳細(xì)日志
pod update --verbose