關(guān)于CocoaPods力穗,是OS X和iOS下的一個第三方類庫管理工具舞虱,它的好處這里不多說,主要說下如何安裝CocoaPods类茂。
CocoaPods的安裝需要Ruby環(huán)境耍属,Mac系統(tǒng)都自帶Ruby,通過下面終端命令查看Ruby版本:
ruby -v
~$ruby -v
ruby 2.3.7p456 (2018-03-28 revision 63024) [universal.x86_64-darwin18]
CocoaPods支持的Ruby最低版本是2.2.2巩检,如果自己的版本低于2.2.2就需要更新升級Ruby:
sudo gem update --system
更換Ruby鏡像
Ruby默認的源地址是國外網(wǎng)絡地址厚骗,通過下面終端命令查看當前鏡像地址:
gem sources -l
~$gem sources -l
/System/Library/Frameworks/Ruby.framework/Versions/2.3/usr/lib/ruby/2.3.0/universal-darwin18/rbconfig.rb:215: warning: Insecure world writable dir /Applications/Cocos/tools/ant/bin in PATH, mode 040777
*** CURRENT SOURCES ***
https://rubygems.org/
首先移除當前鏡像
gem sources --remove https://rubygems.org/
~$gem sources --remove https://rubygems.org/
/System/Library/Frameworks/Ruby.framework/Versions/2.3/usr/lib/ruby/2.3.0/universal-darwin18/rbconfig.rb:215: warning: Insecure world writable dir /Applications/Cocos/tools/ant/bin in PATH, mode 040777
https://rubygems.org/ removed from sources
然后添加國內(nèi)最新Ruby鏡像地址
gem sources -a https://gems.ruby-china.com/
~$gem sources -a https://gems.ruby-china.com/
/System/Library/Frameworks/Ruby.framework/Versions/2.3/usr/lib/ruby/2.3.0/universal-darwin18/rbconfig.rb:215: warning: Insecure world writable dir /Applications/Cocos/tools/ant/bin in PATH, mode 040777
https://gems.ruby-china.com/ added to sources
這時候再查看鏡像地址就是這樣的了
~$gem sources -l
/System/Library/Frameworks/Ruby.framework/Versions/2.3/usr/lib/ruby/2.3.0/universal-darwin18/rbconfig.rb:215: warning: Insecure world writable dir /Applications/Cocos/tools/ant/bin in PATH, mode 040777
*** CURRENT SOURCES ***
https://gems.ruby-china.com/
安裝CocoaPods
Ruby環(huán)境安裝好以后,接下來就是安裝CocoaPods兢哭,終端輸入:
sudo gem install cocoapods
~$sudo gem install cocoapods
Password:
......
Done installing documentation for concurrent-ruby, i18n, thread_safe, tzinfo, activesupport, nap, fuzzy_match, cocoapods-core, claide, cocoapods-deintegrate, cocoapods-downloader, cocoapods-plugins, cocoapods-search, cocoapods-stats, netrc, cocoapods-trunk, cocoapods-try, molinillo, atomos, CFPropertyList, colored2, nanaimo, xcodeproj, escape, fourflusher, gh_inspector, ruby-macho, cocoapods after 33 seconds
28 gems installed
如果終端出現(xiàn)上面的提示领舰,表示安裝成功。到這里還剩最后一步,還需執(zhí)行命令:
pod setup
這一步可能需要耐心等待冲秽,需要下載上百M的文件舍咖。
~$pod setup
/System/Library/Frameworks/Ruby.framework/Versions/2.3/usr/lib/ruby/2.3.0/universal-darwin18/rbconfig.rb:215: warning: Insecure world writable dir /Applications/Cocos/tools/ant/bin in PATH, mode 040777
Setting up CocoaPods master repo
$ /usr/bin/git clone https://github.com/CocoaPods/Specs.git master --progress
Cloning into 'master'...
remote: Enumerating objects: 179, done.
remote: Counting objects: 100% (179/179), done.
remote: Compressing objects: 100% (135/135), done.
remote: Total 2819986 (delta 66), reused 64 (delta 42), pack-reused 2819807
Receiving objects: 100% (2819986/2819986), 614.27 MiB | 834.00 KiB/s, done.
Resolving deltas: 100% (1676501/1676501), done.
Checking out files: 100% (304760/304760), done.
Setup completed
至此,CocoaPods安裝完成锉桑!
使用CocoaPods
在使用之前排霉,我們先驗證下CocoaPods是否安裝成功,搜索一個第三方庫民轴。終端輸入:
~$pod search AFNetworking
正常情況下攻柠,這是會提示Creating search index for spec repo 'master'...
等待一會就會出現(xiàn)搜索結(jié)果了(也可能需要等待很長時間,大約二十幾分鐘后裸,不要著急瑰钮,耐心等待就行??)。
出現(xiàn)上圖這個就說明成功了微驶。直接按“Q”鍵就可以退出浪谴。
接下來,終端進入iOS項目工程的根目錄(就是*.xcodeproj所在的目錄)祈搜,然后創(chuàng)建Podfile文件,終端輸入:
~$touch Podfile
這時工程目錄下多了一個Podfile文件士八。
現(xiàn)在我們要接入Admob廣告SDK容燕,就可以使用CocosPods的方法,用編輯器打開Podfile文件婚度,并將下面的的代碼加到里面:
platform :ios, '8.0'
target 'MyApp' do
pod 'Google-Mobile-Ads-SDK'
end
其中蘸秘,MyApp應該是你項目中的Target名稱。文件保存后蝗茁,在終端中輸入~$pod install
醋虏。
安裝成功后,工程目錄中會生成一個*.xcworkspace文件哮翘。
以后打開工程就打開這個文件颈嚼,不再使用*.xcodeproj文件。
問題報錯
當我們打開*.xcworkspace文件使用Xcode編譯時饭寺,可能會報這么一個錯誤:
diff: /../Podfile.lock: No such file or directory
diff: /Manifest.lock: No such file or directory
error: The sandbox is not in sync with the Podfile.lock. Run 'pod install' or update your CocoaPods installation.