iOS pods集成与使用介绍
CocoaPods簡介
在進行iOS開發的時候,總免不了使用第三方的開源庫,比如SBJson、AFNetworking、Reachability等等。使用這些庫的時候通常需要:
下載開源庫的源代碼并引入工程
向工程中添加開源庫使用到的framework
解決開源庫和開源庫以及開源庫和工程之間的依賴關系、檢查重復添加的framework等問題
如果開源庫有更新的時候,還需要將工程中使用的開源庫刪除,重新執行前面的三個步驟,頓時頭都大了。。。
自從有了CocoaPods以后,這些繁雜的工作就不再需要我們親力親為了,只需要我們做好少量的配置工作,CocoaPods會為我們做好一切!
CocoaPods是一個用來幫助我們管理第三方依賴庫的工具。它可以解決庫與庫之間的依賴關系,下載庫的源代碼,同時通過創建一個Xcode的workspace來將這些第三方庫和我們的工程連接起來,供我們開發使用。
使用CocoaPods的目的是讓我們能自動化的、集中的、直觀的管理第三方開源庫。
(Mac ox 10.11+) CocoaPods安裝,卸載,使用說明
一、全新安裝前,先檢查是否有安裝殘留
由于Mac 10.11更改了安全機制,所以cocoapods得安裝和卸載命令也有所改變,
1、如果之前裝過cocopods,最好先卸載掉,卸載命令:
$ sudo gem uninstall cocoapods【Mac 10.10之前】
$ sudo gem uninstall -n /usr/local/bin cocoapods【Mac 10.11之后】
2、先查看本地安裝過的cocopods相關東西【本步驟沒有必要】,命令如下:
$ gem list --local | grep cocoapods
會顯示如下:
cocoapods-core (0.39.0)
cocoapods-downloader (0.9.3)
cocoapods-plugins (0.4.2)
cocoapods-search (0.1.0)
cocoapods-stats (0.6.2)
cocoapods-trunk (0.6.4)
cocoapods-try (0.5.1)
然后逐個刪除吧:
$ sudo gem uninstall cocoapods-core【Mac 10.10之前】
$ sudo gem uninstall -n /usr/local/bin cocoapods-core【Mac 10.11之后】
。。。
二。安裝
要使用CocoaPods 那就要下載安裝它,而下載安裝CocoaPods需要Ruby環境
1.準備工作
Mac os本身自帶Ruby,但還是更新一下保險,因為我第一次安裝在沒有更新Ruby的情況下就失敗了。
a) 查看下當前ruby版本:打開終端輸入 ruby -v(確實安裝了,不過用這個版本接下來工作失敗了,所以更新下ruby)(更新方法自己百度。。。)
chendeMacBook-Air:~chen$ruby-v
ruby2.3.0p0(2015-12-25revision53290)[x86_64-darwin15]
chendeMacBook-Air:~chen$
chendeMacBook-Air:~chen$ruby-v
ruby2.3.0p0(2015-12-25revision53290)[x86_64-darwin15]
chendeMacBook-Air:~chen$
b)cocoapods鏡像:https://gems.ruby-china.org/好像現在不支持淘寶了
終端輸入如下命令(把Ruby鏡像指向Ruby China,避免被墻,你懂得)
$gem sources --remove https://rubygems.org/
$gem sources --add https://gems.ruby-china.org/
$gem sources -l (用來檢查使用替換鏡像位置成功)
chendeMacBook-Air:~chen$gemsources-l
***CURRENTSOURCES***
https://gems.ruby-china.org/
chendeMacBook-Air:~chen$gemsources-l
***CURRENTSOURCES***
https:2.下載安裝cocoapods
(2)升級gem,在終端輸入命令
$ sudo gem update --system
接著會要求輸入電腦密碼,輸入密碼后回車,就開始升級了。如果看到下面這句話,說明升級成功了。
RubyGems system software updated
還有一點需要注意,pod setup在執行時,會輸出Setting up CocoaPods master repo,但是會等待比較久的時間。這步其實是 Cocoapods 在將它的信息下載到~/.cocoapods目錄下,如果你等太久,可以試著 cd 到那個目錄,用du -sh *來查看下載進度。你也可以參考本文接下來的使用 cocoapods 的鏡像索引一節的內容來提高下
載速度。
安裝過程遇到的問題:
1、Ruby版本過低(更新就行了)
2、
$podsetup
SettingupCocoaPodsmasterrepo
[!]/usr/bin/gitclonehttps://github.com/CocoaPods/Specs.gitmaster
Cloninginto‘master’…
error:RPCfailed;curl56SSLRead()returnerror-36
fatal:Theremoteendhungupunexpectedly
fatal:earlyEOF
fatal:index-packfailed
$podsetup
SettingupCocoaPodsmasterrepo
[!]/usr/bin/gitclonehttps://github.com/CocoaPods/Specs.gitmaster
Cloninginto‘master’…
error:RPCfailed;curl56SSLRead()returnerror-36
fatal:Theremoteendhungupunexpectedly
fatal:earlyEOF
fatal:index-packfailed
3、
$podsetup
SettingupCocoaPodsmasterrepo
[!]/usr/bin/gitclonehttps://github.com/CocoaPods/Specs.gitmaster
Cloninginto‘master’…
error:RPCfailed;curl56SSLRead()returnerror-9806
fatal:Theremoteendhungupunexpectedly
fatal:earlyEOF
fatal:index-packfailed
$podsetup
SettingupCocoaPodsmasterrepo
[!]/usr/bin/gitclonehttps://github.com/CocoaPods/Specs.gitmaster
Cloninginto‘master’…
error:RPCfailed;curl56SSLRead()returnerror-9806
fatal:Theremoteendhungupunexpectedly
fatal:earlyEOF
fatal:index-packfailed
4、
$podsetup
SettingupCocoaPodsmasterrepo
[!]/usr/bin/gitclonehttps://github.com/CocoaPods/Specs.gitmaster
Cloninginto‘master’…
error:RPCfailed;curl18transferclosedwithoutstandingreaddataremaining
fatal:Theremoteendhungupunexpectedly
fatal:earlyEOF
fatal:index-packfailed
$podsetup
SettingupCocoaPodsmasterrepo
[!]/usr/bin/gitclonehttps://github.com/CocoaPods/Specs.gitmaster
Cloninginto‘master’…
error:RPCfailed;curl18transferclosedwithoutstandingreaddataremaining
fatal:Theremoteendhungupunexpectedly
fatal:earlyEOF
fatal:index-packfailed
解決方法:上面錯誤主要是os10.11+和cocoapods1.0+版本升級的安全機制更改造成的。主要出現在pod setup過程中。且該過程等待時間非常長,所以,你可以在$sudo gem install -n /usr/local/bin cocoapods命令后直接執行$Gitclone https://git.coding.net/CocoaPods/Specs.git ~/.cocoapods/repos/master 該過程作用與 pod setup作用相同,作者親測。效果極佳?。。。?/p>
可能出的錯
1.
解決辦法
sudo xcode-select -switch /Applications/Xcode-beta.app/Contents/Developer
后面的地址你可以打開Xcode顯示包內容,找到那個文件夾拖到終端里面比較不容易錯。
2.
官網是這樣給推薦的:
在創建Podfile的時候,用這種格式使用,
3.
解決辦法:不要使用文本編輯去編輯Podfile,使用Xcode編輯,或者使用終端敲命令去編輯?;蛘咻斎敫袷藉e誤,沒輸入運行版本:$platform:ios, ‘9.0‘
三:使用CocoaPods
終端中 cd到項目路徑
建立Podfile(配置文件)
$vim Podfile
鍵盤輸入 i 進入編輯模式,輸入
platform :ios, '7.0'
pod 'MBProgressHUD', '~> 0.8’(之前的,我的電腦上已經無效了,報錯: [!] The dependency `MBProgressHUD (~> 0.8)` is not used in any concrete target.)
(現在使用:MyApp 替換成自己的項目名)
target 'MyApp' do
pod 'AFNetworking', '~> 2.6'
pod 'ORStackView', '~> 3.0'
end
然后按Esc,并且輸入“ :”號進入vim命令模式,然后在冒號后邊輸入wq
再輸入 $pod install
為了確定AFNetworking是否支持CocoaPods,可以用CocoaPods的搜索功能驗證一下。在終端中輸入:
$ pod search AFNetworking
2)出現如下警告
[!] Your Podfile has had smart quotessanitised. To avoid issues in the future, you should not use TextEdit forediting it. If you are not using TextEdit, you should turn off smart quotes inyour editor of choice.
解決辦法:不要使用文本編輯去編輯Podfile,使用Xcode編輯,或者使用終端敲命令去編輯。
(3) 執行pod install 或pod update 命令后,updating local specsrepositories 卡住不動
解決辦法:pod install 被墻了,換成新的命令pod install --verbose --no-repo-update
(4) 項目運行時報錯:Thefile “Pods.xcconfig” couldn’t be opened because there is no such file.
解決辦法:pod update就可以了。
關于 Podfile.lock
當你執行pod install之后,除了 Podfile 外,CocoaPods 還會生成一個名為Podfile.lock的文件,Podfile.lock 應該加入到版本控制里面,不應該把這個文件加入到.gitignore中。因為Podfile.lock會鎖定當前各依賴庫的版本,之后如果多次執行pod install不會更改版本,要pod update才會改Podfile.lock了。這樣多人協作的時候,可以防止第三方庫升級時造成大家各自的第三方庫版本不一致。
看過其他教程的人一定會奇怪,很多教程中用的是pod update 命令,為什么你一直在提pod install,因為pod update會更新的你Podfile.lock文件到最新版本,接下來的害處就如上段所說。
所以千萬記住,無論是自己的項目,還是跑別人的源碼,一定要用pod install,至于pod update,看你自己是不是要三方升級類庫(我還是建議最好在Podfile中指定版本,想要升級,只需更改Podfile中的版本號,再pod install)。
11、pod update
若果Podfile中指定的依賴庫版本不是寫死的,當對應的依賴庫有了更新,無論有沒有Podfile.lock文件都會去獲取Podfile文件描述的允許獲取到的最新依賴庫版本。
pod'AFNetworking'//不顯式指定依賴庫版本,表示每次都獲取最新版本(<spanstyle="color:rgb(50,62,50);font-family:Arial;font-size:14px;line-height:26px;">11、</span><spanstyle="color:rgb(50,62,50);font-family:Arial;font-size:14px;line-height:26px;">podupdate</span>)
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'//使用最新版本,與不顯示指定依賴庫版本相同
總結
以上是生活随笔為你收集整理的iOS pods集成与使用介绍的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Compaction介绍
- 下一篇: 怎么创建具有真实纹理的CG场景岩石?