1.CocoaPods簡介
隨著IOS開發(fā)者的增多,業(yè)界也出現(xiàn)了為IOS程序提供依賴管理的工具,它的名字叫作CocoaPods(http://cocoapods.org/).
GitHub地址:https://github.com/CocoaPods/CocoaPods
2.CocoaPods的安裝
CocoaPods的安裝方式比較簡單,Mac下自帶ruby,使用ruby的gem命令即可下載安裝:
$ sudo gem install cocoapods
$ pod setup
另外,ruby的軟件源rubygems.org因為使用亞馬遜的云服務(wù),所以被屏蔽了,需要更新一下ruby的源,下面的代碼將官方的ruby源替換成國內(nèi)淘寶的源:
gem sources --remove https://rubygems.org/
gem sources -a https://ruby.taobao.org/
gem sources -l
還有一點需要注意,pod setup在執(zhí)行時,會輸出Setting up CocoaPods master repo
,但是會等待比較久的時間,這步其實是CocoaPods在將它的信息下載到~/.cocoapods
目錄下,如果你等太久,可以試著cd到那個目錄,用du -sh *
來查看下載進(jìn)度.
3.使用CocoaPods的鏡像索引
所有項目的Podspec文件都托管在https://github.com/CocoaPods/Specs. 第一次執(zhí)行pod setup
時,CocoaPods會將這些Podspec索引文件更新到本地的~/.cocoapods/
目錄下,這個索引文件比較大,有80MB左右,所以第一次更新時非常慢.
一個叫akinliu(http://akinliu.github.io/2014/05/03/cocoapods-specs-/) 的朋友在gitcafe(http://gitcafe.com/) 和occhina(http://www.oschina.net/) 上建立了CocoaPods索引庫的鏡像,因為gitcafe和occhina都是國內(nèi)服務(wù)器,所以在執(zhí)行索引更新操作時,會快很多.如下操作可以將CocoaPods設(shè)置成使用gitcafe鏡像:
pod repo remove master
pod repo add master https://gitcafe.com/akuandev/Specs.git
pod repo update
將以上代碼中的https://gitcafe.com/akuandev/Specs.git
替換成http://git.oschina.net/akuandev/Specs.git
即可使用occhina上的鏡像.
4.使用CocoaPods
使用時需要新建一個名為Podfile
的文件,以如下格式,將依賴的庫名字依次列在文件中即可:(示例:JSONKit和AFNetworking)
platform :ios
pod 'JSONKit','~> 1.5pre'
pod 'AFNetworking','~> 2.5.0'
然后將編輯好的Podfile文件放到項目根目錄中,執(zhí)行如下命令即可:
cd "你的項目根目錄"
pod install
現(xiàn)在,所有第三方庫都已經(jīng)下載完成并且設(shè)置好了編譯參數(shù)和依賴,你只需要記住如下兩點即可:
- 使用CocoaPods生成的
*.xcworkspace
文件來打開工程,而不是以前的*.xcodeproj
文件 - 每次更改了Podfile文件,都需要重新執(zhí)行一次
pod update
命令
5.查找第三方庫
如果不知道CocoaPods管理的庫中是否有你想要的庫,那么你可以通過pod search
命令進(jìn)行查找,以下是我用pod search AFNetworking
查找到的所有可用的庫:
-> AFNetworking (2.5.0)
A delightful iOS and OS X networking framework.
pod 'AFNetworking', '~> 2.5.0'
- Homepage: https://github.com/AFNetworking/AFNetworking
- Source: https://github.com/AFNetworking/AFNetworking.git
- Versions: 2.5.0, 2.4.1, 2.4.0, 2.3.1, 2.3.0, 2.2.4, 2.2.3, 2.2.2, 2.2.1,
2.2.0, 2.1.0, 2.0.3, 2.0.2, 2.0.1, 2.0.0, 2.0.0-RC3, 2.0.0-RC2, 2.0.0-RC1,
1.3.4, 1.3.3, 1.3.2, 1.3.1, 1.3.0, 1.2.1, 1.2.0, 1.1.0, 1.0.1, 1.0, 1.0RC3,
1.0RC2, 1.0RC1, 0.10.1, 0.10.0, 0.9.2, 0.9.1, 0.9.0, 0.7.0, 0.5.1 [master
repo]
- Sub specs:
- AFNetworking/Serialization (2.5.0)
- AFNetworking/Security (2.5.0)
- AFNetworking/Reachability (2.5.0)
- AFNetworking/NSURLConnection (2.5.0)
- AFNetworking/NSURLSession (2.5.0)
- AFNetworking/UIKit (2.5.0)
-> AFNetworking+AutoRetry (0.0.5)
Auto Retries for AFNetworking requests
pod 'AFNetworking+AutoRetry', '~> 0.0.5'
- Homepage: https://github.com/shaioz/AFNetworking-AutoRetry
- Source: https://github.com/shaioz/AFNetworking-AutoRetry.git
- Versions: 0.0.5, 0.0.4, 0.0.3, 0.0.2, 0.0.1 [master repo]
-> AFNetworking+Ext (1.1)
AFNetworking的封裝, 并提供一個 UIImageView+DYLoading cache in fileSystem+memory
pod 'AFNetworking+Ext', '~> 1.1'
- Homepage: https://github.com/junhaiyang/AFNetworkingExt
- Source: https://github.com/junhaiyang/AFNetworkingExt.git
- Versions: 1.1, 1.0, 0.5, 0.4, 0.3 [master repo]
- Sub specs:
- AFNetworking+Ext/Base (1.1)
- AFNetworking+Ext/AFCustomRequestOperation (1.1)
- AFNetworking+Ext/AFDownloadRequestOperation (1.1)
- AFNetworking+Ext/AFTextResponseSerializer (1.1)
- AFNetworking+Ext/example (1.1)
- AFNetworking+Ext/UIKit (1.1)
- AFNetworking+Ext/UIKit/UIImageView+DYLoading (1.1)
-> AFNetworking+ImageActivityIndicator (1.0.1)
AFNetworking+ImageActivityIndicator makes it easy to show an activity
indicator while an image view's image is loading using AFNetworking.
pod 'AFNetworking+ImageActivityIndicator', '~> 1.0.1'
- Homepage:
https://github.com/JRG-Developer/AFNetworking-ImageActivityIndicator
- Source:
https://github.com/JRG-Developer/AFNetworking-ImageActivityIndicator.git
- Versions: 1.0.1, 1.0.0 [master repo]
-> AFNetworking+SingleBlock (0.0.1)
Categories that aggregate the success and failure block in one single
solution
pod 'AFNetworking+SingleBlock', '~> 0.0.1'
- Homepage: https://github.com/acerbetti/AFNetworking-SingleBlock
- Source: https://github.com/acerbetti/AFNetworking-SingleBlock.git
- Versions: 0.0.1 [master repo]
-> AFNetworking+streaming (0.6)
A very quick extension to AFNetworking that adds stream based parsing
pod 'AFNetworking+streaming', '~> 0.6'
- Homepage: https://github.com/deanWombourne/AFNetworking-streaming
- Source: https://github.com/deanWombourne/AFNetworking-streaming.git
- Versions: 0.6, 0.5, 0.4, 0.3, 0.2, 0.1 [master repo]
- Sub specs:
- AFNetworking+streaming/Core (0.6)
- AFNetworking+streaming/Json (0.6)
// ...此處省略若干行
@唐巧 <IOS開發(fā)進(jìn)階>
End