cocoapods 聽起來很高大上,其實就是一個用來管理第三方庫的一個家伙。
怎么使用呢?在我集合了兩張博文和自己的實踐后,總結出來:兩篇鏈接如下(謝謝博主): http://blog.csdn.net/sanjunsheng/article/details/28398455 和 http://www.360doc.com/content/14/0309/10/11029609_358970353.shtml 有了這個工具,它就能幫我們好好地管理第三方庫了。
它解決了庫與庫之間的依賴關系,同時通過創建一個Xcode的workspace來將這些第三方庫和我們的工程連接起來,供我們開發使用。
使用CocoaPods的目的是讓我們能自動化的、集中的、直觀的管理第三方開源庫。
1.安裝:
CocoaPods是使用Ruby實現的,要想使用它需要有Ruby的環境,幸運的是OS X系統默認的已經可以運行Ruby了,因此我們只需要直接下載運行即可
1.1 如果直接安裝,肯定是不可行的。首先我們必須先把原來的RubyGems移除掉,原因很簡單:因為我們鏈接不上;萬能的馬雲替我們完成了這一偉大的工程。
原理:先移除掉系統默認的,改為淘寶的就可以了。代碼如下:
gem sources --remove https://rubygems.org/
提示如下:
/System/Library/Frameworks/Ruby.framework/Versions/2.0/usr/lib/ruby/2.0.0/universal-darwin14/rbconfig.rb:213: warning: Insecure world writable dir /usr/local in PATH, mode 040777 source https://rubygems.org/ not present in cache
這個是因為我之前把它給刪掉了一次;
1.2 添加淘寶的:
gem sources -a https://ruby.taobao.org/
提示如下:
/System/Library/Frameworks/Ruby.framework/Versions/2.0/usr/lib/ruby/2.0.0/universal-darwin14/rbconfig.rb:213: warning: Insecure world writable dir /usr/local in PATH, mode 040777 https://ruby.taobao.org/ added to sources
最后一行,代表着淘寶的鏡像已經安裝上了。如果不是這個結果,請百度之~
1.3 查看目前所有的鏡像文件:
gem sources -l
提示如下:
/System/Library/Frameworks/Ruby.framework/Versions/2.0/usr/lib/ruby/2.0.0/universal-darwin14/rbconfig.rb:213: warning: Insecure world writable dir /usr/local in PATH, mode 040777 *** CURRENT SOURCES *** https://ruby.taobao.org/
最后一行,代表所有的鏡像文件只有淘寶。
1.4 鏡像文件已經搭建好了,那么就讓我們安裝CocoaPods吧:
gem install cocoapods
提示如下:
/System/Library/Frameworks/Ruby.framework/Versions/2.0/usr/lib/ruby/2.0.0/universal-darwin14/rbconfig.rb:213: warning: Insecure world writable dir /usr/local in PATH, mode 040777 Fetching: i18n-0.7.0.gem (100%) ERROR: While executing gem ... (Gem::FilePermissionError) You don't have write permissions for the /Library/Ruby/Gems/2.0.0 directory.
它告訴我:我沒有這個權限。。(如果有其他報錯,比如: Could not find a valid gem 'coocapods' (>= 0) in any repository,也是沒有權限的原因)百度之之后,這么解決:
sudo chmod 777 /Library/Ruby/Gems/2.0.0/
需要輸入密碼。輸完密碼后,繼續安裝。
1.5 重新安裝
sudo gem install cocoapods
1.6 又報錯了。gem的版本過低(2016.8.31)
ERROR: While executing gem … (Errno::EACCES) Permission denied - /Library/Ruby/Gems/2.0.0/cache/i18n-0.7.0.gem
解決辦法:
gem update –system
1.7 又提示我ruby版本過低
activesupport requires Ruby version >= 2.2.2
然后去升級Ruby:http://blog.csdn.net/lissdy/article/details/9191351
1.8重新安裝
sudo gem install cocoapods
提示如下:(一部分)
/System/Library/Frameworks/Ruby.framework/Versions/2.0/usr/lib/ruby/2.0.0/universal-darwin14/rbconfig.rb:213: warning: Insecure world writable dir /usr/local in PATH, mode 040777 Fetching: i18n-0.7.0.gem (100%) Successfully installed i18n-0.7.0 Fetching: thread_safe-0.3.5.gem (100%) Successfully installed thread_safe-0.3.5 Fetching: tzinfo-1.2.2.gem (100%) Successfully installed tzinfo-1.2.2
。。。。還有很多,代表着正在安裝。。
等到結束后,我們會看到:
Installing ri documentation for cocoapods-0.38.2 20 gems installed
這代表着就結束了。。
1.6 設置cocoapods
pod setup
提示如下:
/System/Library/Frameworks/Ruby.framework/Versions/2.0/usr/lib/ruby/2.0.0/universal-darwin14/rbconfig.rb:213: warning: Insecure world writable dir /usr/local in PATH, mode 040777 Setting up CocoaPods master repo CocoaPods 0.39.0.beta.4 is available. To update use: `gem install cocoapods --pre` [!] This is a test version we'd love you to try. For more information see http://blog.cocoapods.org and the CHANGELOG for this version http://git.io/BaH8pQ. Setup completed
代表着設置結束。
1.7 去設置CocoaPods的項目文件,進入我們的項目,cd (項目目錄) eg“cd /Users/KuaiYong/Desktop/exercises/cocoapodsTest”我項目的文件夾是:cocoapodsTest
1.8 進入目錄后,點擊ls,就得能你的.xcodeproj才行,否則就是目錄不對,得去調整;
1.9 建立Podfile文件,我對它的理解是:在這里面說明要引入哪些文件。
touch Podfile
這樣,在目錄下就會有這個文件。
1.10 編輯此Podfile文件,因為我們已經進入了項目文件,所以可以直接編輯:(也可以不使用vi,直接用記事本或者編輯器進行編輯保存)
vi Podfile
輸入i就是開始編輯了,輸入的內容如下:
platform :ios, '6.0' pod 'JSONKit' pod "AFNetworking'
按ESC鍵,再按住shift加冒號,再在Terminal里面輸入wq就寫入加退出了。
1.11 引入
pod install
提示如下:
/System/Library/Frameworks/Ruby.framework/Versions/2.0/usr/lib/ruby/2.0.0/universal-darwin14/rbconfig.rb:213: warning: Insecure world writable dir /usr/local in PATH, mode 040777 Updating local specs repositories CocoaPods 0.39.0.beta.4 is available. To update use: `gem install cocoapods --pre` [!] This is a test version we'd love you to try. For more information see http://blog.cocoapods.org and the CHANGELOG for this version http://git.io/BaH8pQ. Analyzing dependencies Downloading dependencies Installing AFNetworking (0.10.1) Installing JSONKit (1.4) Generating Pods project Integrating client project [!] Please close any current Xcode sessions and use `cocoapodsTest.xcworkspace` for this project from now on. Sending stats
這樣就完成了。將XCode退出,再打開后來生成的.xcworkspace文件就可以看到整個項目了。
1.12bug出現: direct access to Objective-C's isa is deprecated in favor of object_getClass()十有八九會出現這個,反正我是出現了。解決辦法:
將cocoapods自動生成的Pods項目文件的Build Settings 從項目中搜索 Direct usage of 'isa' 將 YES(treat as error) 改為NO就可以了。原本項目本身的不用管。
這樣,再在原來的項目里引入第三方庫就可以了。和本身導入文件是一樣的。
OVER。
11月3日 更新。經博友留言才知曉,一試果然不能用了。而且淘寶鏡像的首頁已經貼出了通知。
12.9日更新:
1.如何移除CocoaPods:
1.1 which pod
會告訴我們pod安裝在哪個路徑
1.2 sudo rm -rf path
提示我們輸入密碼,然后這個cocoapods就被移除了,在搜索時,使用pod就沒有反應了。