簡介
CocoaPods is the dependency manager for Swift and Objective-C Cocoa projects. It has over ten thousand libraries and can help you scale your projects elegantly.
CocoaPods 是 Cocoa 項(xiàng)目的依賴管理工具。它支持 Swift 和 Objective-C 語言,同時(shí)支持超過10000個(gè)第三方開源庫。它可以幫助你管理項(xiàng)目,讓你的項(xiàng)目更加優(yōu)美。
每種語言發(fā)展到一個(gè)階段,就會(huì)出現(xiàn)相應(yīng)的依賴管理工具,例如 Java 語言的 Maven,nodejs 的 npm。隨著 iOS 開發(fā)者的增多,CocoaPods的出現(xiàn)是必然的。
安裝CocoaPods
CocoaPods is built with Ruby and is installable with the default Ruby available on OS X. We recommend you do this.
Using the default Ruby install will require you to use sudo when installing gems. Further instructions are in the guides.
CocoaPods 管理在 Ruby 中??梢允褂貌⑼扑]使用 OS X 自帶的 Ruby 安裝。當(dāng)你使用自帶的 Ruby 安裝 gems 時(shí)需要使用 sudo 命令。更詳細(xì)的說明可以再官方文檔中查看。
簡單來說,在 OS X 下安裝 CocoaPods 異常的簡單,只需要執(zhí)行如下的命令即可:
$ sudo gem install cocoapods
$ pod setup
如果你的 gem 版本過低(系統(tǒng)版本低于 OS X Lion),安裝可能會(huì)出現(xiàn)問題??梢允褂萌缦旅畈榭?gem 版本。
$ gem -v
如有必要,可以使用如下命令升級(jí)。
$ sudo gem update --system
ruby 的軟件源 https://rubygems.org 使用的是亞馬遜的云服務(wù),所以被墻了,還需要使用如下代碼將官方的 ruby 源替換成國內(nèi)淘寶的源:
$ gem sources --remove https://rubygems.org/
$ gem sources -a http://ruby.taobao.org/
$ gem sources -l
*使用CocoaPods的鏡像索引
本段摘自唐巧的技術(shù)博客
還有一點(diǎn)需要注意,pod setup 在執(zhí)行時(shí),會(huì)輸出 Setting up CocoaPods master repo,但是會(huì)等待比較久的時(shí)間。這步其實(shí)是 Cocoapods 在將它的信息下載到 ~/.cocoapods 目錄下,如果你等太久,可以試著 cd 到那個(gè)目錄,用 du -sh * 來查看下載進(jìn)度。你也可以使用 CocoaPods 的鏡像索引來提高下載速度。
所有的項(xiàng)目的 Podspec 文件都托管在 https://github.com/CocoaPods/Specs。第一次執(zhí)行pod setup時(shí),CocoaPods 會(huì)將這些podspec索引文件更新到本地的 ~/.cocoapods 目錄下,這個(gè)索引文件比較大,有 80M 左右。所以第一次更新時(shí)非常慢,筆者就更新了將近一個(gè)小時(shí)才完成。
一個(gè)叫 akinliu 的朋友在 gitcafe 和 oschina 上建立了 CocoaPods 索引庫的鏡像,因?yàn)?gitcafe 和 oschina 都是國內(nèi)的服務(wù)器,所以在執(zhí)行索引更新操作時(shí),會(huì)快很多。如下操作可以將 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 即可使用 oschina 上的鏡像。
使用CocoaPods管理項(xiàng)目
首先,搜索需要讓 CocoaPods 管理的庫,例如:
$ pod search AFNetWorking
搜索結(jié)果如下:
-> AFNetworking (2.6.0)
A delightful iOS and OS X networking framework.
pod 'AFNetworking', '~> 2.6.0'
- Homepage: https://github.com/AFNetworking/AFNetworking
- Source: https://github.com/AFNetworking/AFNetworking.git
- Versions: 2.6.0, 2.5.4, 2.5.3, 2.5.2, 2.5.1, 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]
- Subspecs:
- AFNetworking/Serialization (2.6.0)
- AFNetworking/Security (2.6.0)
- AFNetworking/Reachability (2.6.0)
- AFNetworking/NSURLConnection (2.6.0)
- AFNetworking/NSURLSession (2.6.0
- AFNetworking/UIKit (2.6.0)
Search for pods (above). Then list the dependencies in a text file named Podfile in your Xcode project directory:
然后在你Xcode項(xiàng)目目錄里名為 Podfile 的文本文件中列出每個(gè)搜索結(jié)果中的如下語句:
“pod 'AFNetworking', '~> 2.6.0'”。
Tip: CocoaPods provides a pod init command to create a Podfile with smart defaults. You should use it.
提示:你可以使用CocoaPods提供 pod init 命令去創(chuàng)建默認(rèn)的 Podfile,效果如下:
# Uncomment this line to define a global platform for your project
# platform :ios, '6.0'
target 'App' do
end
Now you can install the dependencies in your project:
現(xiàn)在,你可以使用如下命令在你的安裝你的依賴項(xiàng)了。
$ pod install
安裝過程中,CocoPods 會(huì)使用遞歸來分析所有的需求,并且建立一個(gè)代碼相關(guān)性的圖,最后將 Podfile 序列化為 Podfile.lock。比如,如果兩個(gè)庫都需要使用 AFNetworking,CocoaPods會(huì)確定一個(gè)同時(shí)能被這兩庫使用的版本,然后將同一個(gè)安裝版本鏈接到兩個(gè)不同的庫中。
同時(shí),CocoaPods 會(huì)創(chuàng)建一個(gè)新的包含之前安裝好的靜態(tài)庫 Xcode 項(xiàng)目,然后將它們鏈接成一個(gè)新的 libPods.a target。你原有的項(xiàng)目將會(huì)依賴這個(gè)新的靜態(tài)庫。并且會(huì)同時(shí)創(chuàng)建 xcworkspace 文件。
反復(fù)使用 pod install 命令,只會(huì)讓 CocoaPods 重復(fù)以上步驟,重新安裝這些庫。所以,當(dāng)你需要升級(jí)它們時(shí),請使用以下命令:
$ pod update
Make sure to always open the Xcode workspace instead of the project file when building your project:
此后每次編譯項(xiàng)目的時(shí)候,確保都是通過 Xcode workspace 打開項(xiàng)目而不是通過 project file 打開。
$ open App.xcworkspace
Now you can import your dependencies e.g.:#import <Reachability/Reachability.h>
現(xiàn)在,你可以導(dǎo)入你的依賴項(xiàng)例如:#import <Reachability/Reachability.h>
博客:xuyafei.cn
簡書:jianshu.com/users/2555924d8c6e
微博:weibo.com/xuyafei86
Github:github.com/xiaofei86