一、概要
iOS開發(fā)時,項目中會引用許多第三方庫,CocoaPods(https://github.com/CocoaPods/CocoaPods)可以用來方便的統(tǒng)一管理這些第三方庫。
二、安裝
由于網(wǎng)上的教程基本都大同小異,但細節(jié)之處還不是很完善,所以借機會在這里補充下:
注:要使用CocoaPods,那就要下載安裝它,而下載安裝CocoaPods需要Ruby環(huán)境
1、Ruby環(huán)境搭建
當(dāng)前安裝環(huán)境為Mac mini 10.8.5。Mac? OS本身自帶Ruby,但還是更新一下保險,因為我第一次安裝在沒有更新Ruby的情況下就失敗了。
a 查看下當(dāng)前ruby版本:打開終端輸入 ruby -v(確實安裝了,不過用這個版本接下來工作失敗了,所以更新下ruby)
[objc] view plaincopyprint?在CODE上查看代碼片派生到我的代碼片
ritekiMac-mini:PodTest lucky$ ruby -v
ruby 1.8.7 (2012-02-08 patchlevel 358) [universal-darwin12.0]
ritekiMac-mini:PodTest lucky$
b 更新ruby
終端輸入如下命令(把Ruby鏡像指向taobao,避免被墻,你懂得)
gem sources --remove https://rubygems.org/
gem sources -a https://ruby.taobao.org/
gem sources -l? (用來檢查使用替換鏡像位置成功)
[objc] view plaincopyprint?在CODE上查看代碼片派生到我的代碼片
ritekiMac-mini:~ lucky$ gem sources --remove https://rubygems.org/
https://rubygems.org/ removed from sources
ritekiMac-mini:~ lucky$ gem sources -a https://ruby.taobao.org/
https://ruby.taobao.org/ added to sources
ritekiMac-mini:~ lucky$ gem sources -l
*** CURRENT SOURCES ***
b 更新ruby
終端輸入如下命令(把Ruby鏡像指向taobao,避免被墻,你懂得)
gem sources --remove https://rubygems.org/
gem sources -a https://ruby.taobao.org/
gem sources -l? (用來檢查使用替換鏡像位置成功)
[objc] view plaincopyprint?在CODE上查看代碼片派生到我的代碼片
ritekiMac-mini:~ lucky$ gem sources --remove https://rubygems.org/
https://rubygems.org/ removed from sources
ritekiMac-mini:~ lucky$ gem sources -a https://ruby.taobao.org/
https://ruby.taobao.org/ added to sources
ritekiMac-mini:~ lucky$ gem sources -l
*** CURRENT SOURCES ***
2、下載安裝CocoaPods
終端輸入:sudo gem install cocoapods
[html] view plaincopyprint?在CODE上查看代碼片派生到我的代碼片
ritekiMac-mini:~ lucky$ sudo gem install cocoapods
CHANGELOG:
## 0.32.1
##### Bug Fixes
* Fixed the Podfile `default_subspec` attribute in nested subspecs.
[Fabio Pelosin][irrationalfab]
\ [#2050](https://github.com/CocoaPods/CocoaPods/issues/2050)
Successfully installed cocoapods-0.32.1
Installing ri documentation for cocoapods-0.32.1
/System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/1.8/rdoc/rdoc.rb:280: warning: conflicting chdir during another chdir block
/System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/1.8/rdoc/rdoc.rb:287: warning: conflicting chdir during another chdir block
Done installing documentation for cocoapods after 10 seconds
1 gem installed
這樣就下載安裝好了CocoaPods
3、使用CocoaPods
a? 新建一個項目,名字PodTest
b? 終端中,cd到項目總目錄(注意:包含PodTest文件夾、PodTest.xcodeproj、PodTestTest的那個總目錄)
cd /Users/lucky/Desktop/PodTest
c? 建立Podfile(配置文件)
接著上一步,終端輸入 vim Podfile
鍵盤輸入 i,進入編輯模式,輸入
platform :ios, '7.0'
pod 'MBProgressHUD', '~> 0.8'
然后按Esc,并且輸入“ :”號進入vim命令模式,然后在冒號后邊輸入wq
注意:鍵盤輸入 :后,才能輸入wq。回車后發(fā)現(xiàn)PodTest項目總目錄中多一個Podfile文件
激動人心的時刻到了:確定終端cd到項目總目錄,然后輸入 pod install,等待一會,大約3分鐘。
查看項目根目錄:
注意:現(xiàn)在打開項目不是點擊 PodTest.xodeproj了,而是點擊 PodTest.xcworkspace
打開項目后看到項目結(jié)構(gòu)并且測試一下:
補充:
1、CocoaPods的基本安裝及使用都詳細的說明了,但還有一些補充,當(dāng)需要同時導(dǎo)入多個第三方時候怎么辦 ?
這就需要修改Podfile了,就是用vim編輯的那個保存在項目根目錄中的文件,修改完了Podfile文件,需要重新執(zhí)行一次pod install命令。
例如:
platform :ios
pod 'JSONKit',? ? ? '~> 1.4'
pod 'AFNetworking',? '~> 2.0'
本文章盜竊自“有夢想的蝸牛”的博客,地址:http://blog.csdn.net/showhilllee/article/details/38398119