CocoaPods是應(yīng)用級(jí)依賴項(xiàng)管理器(dependency manager),用于管理Objective-C、Swift語言應(yīng)用的依賴項(xiàng)。由Eloy Durán 和 Fabio Pelosin于2011年開發(fā)完成。
1. 安裝CocoaPods
CocoaPods是使用Ruby語言編寫的,macOS系統(tǒng)默認(rèn)安裝了Ruby。CocoaPods建議使用默認(rèn)的Ruby安裝CocoaPods,除非你對(duì)Ruby有所了解。
使用如下命令安裝CocoaPods:
$ sudo gem install cocoapods
使用系統(tǒng)自帶Ruby安裝CocoaPods時(shí),需要使用
sudo權(quán)限。
輸入上面命令后,命令行提示如下:
Fetching: concurrent-ruby-1.0.5.gem (100%)
Successfully installed concurrent-ruby-1.0.5
Fetching: i18n-0.9.1.gem (100%)
Successfully installed i18n-0.9.1
...
Installing ri documentation for cocoapods-1.3.1
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, colored2, nanaimo, xcodeproj, escape, fourflusher, gh_inspector, ruby-macho, cocoapods after 31 seconds
26 gems installed
使用如下命令查看CocoaPods版本:
$ gem list --local | grep cocoapods
cocoapods (1.3.1)
cocoapods-core (1.3.1)
cocoapods-deintegrate (1.0.1)
cocoapods-downloader (1.1.3)
cocoapods-plugins (1.0.0)
cocoapods-search (1.0.0)
cocoapods-stats (1.0.0)
cocoapods-trunk (1.3.0)
cocoapods-try (1.1.0)
可以看到cocoapods版本為1.3.1,同時(shí)也可以看到其它部分版本。
默認(rèn)安裝的CocoaPods為正式版,使用
gem install cocoapods --pre命令安裝測(cè)試版CocoaPods,使用gem install cocoapods -v x.y.z命令安裝指定版本CocoaPods。
2. 更新CocoaPods
只需要再次安裝,就可以將CocoaPods升級(jí)到最新版:
$ [sudo] gem install cocoapods
如果安裝CocoaPods時(shí)使用了sudo權(quán)限,更新時(shí)也需要sudo權(quán)限。
如果CocoaPods有了新版本,在你使用pod命令時(shí)會(huì)得到CocoaPods X.X.X is now available, please update.的提示信息。
3. 移除低版本CocoaPods
CocoaPods更新到新版本后,低版本的CocoaPods并不會(huì)被自動(dòng)移除。如下所示:
$ gem list --local | grep cocoapods
cocoapods (1.3.1, 1.1.0)
cocoapods-core (1.3.1, 1.1.0)
cocoapods-deintegrate (1.0.1)
cocoapods-downloader (1.1.3)
cocoapods-plugins (1.0.0)
cocoapods-search (1.0.0)
cocoapods-stats (1.0.0)
cocoapods-trunk (1.3.0)
cocoapods-try (1.1.0)
使用如下命令移除指定版本CocoaPods:
$ gem uninstall cocoapods -v 1.1.0
Successfully uninstalled cocoapods-1.1.0
$ gem uninstall cocoapods-core -v 1.1.0
Successfully uninstalled cocoapods-core-1.1.0
// 查看cocoapods版本。
$ gem list --local | grep cocoapods
cocoapods (1.3.1)
cocoapods-core (1.3.1)
cocoapods-deintegrate (1.0.1)
cocoapods-downloader (1.1.3)
cocoapods-plugins (1.0.0)
cocoapods-search (1.0.0)
cocoapods-stats (1.0.0)
cocoapods-trunk (1.3.0)
cocoapods-try (1.1.0)
4. 管理第三方庫
4.1 創(chuàng)建Podfile文件
CocoaPods中Podfile 文件用于列出要在項(xiàng)目中使用的庫,可以使用以下兩種方式創(chuàng)建Podfile文件:
- 在項(xiàng)目目錄中創(chuàng)建一個(gè)名為
Podfile的空文本文件。 - 將Terminal定位到項(xiàng)目目錄,輸入
pod init命令,CocoaPods會(huì)自動(dòng)創(chuàng)建Podfile文件,并會(huì)自動(dòng)初始化一些必要代碼。推薦使用pod init命令創(chuàng)建Podfile文件。
使用pod init命令創(chuàng)建的Podfile文件內(nèi)容如下:
# Uncomment the next line to define a global platform for your project
# platform :ios, '9.0'
target 'MyApp' do
# Uncomment the next line if you're using Swift or would like to use dynamic frameworks
# use_frameworks!
# Pods for MyApp
target 'MyAppTests' do
inherit! :search_paths
# Pods for testing
end
end
4.2 列出所需第三方庫
Podfile文件內(nèi)容可以非常簡(jiǎn)單,如下所示:
target 'MyApp` do
pod 'Mantle'
end
下面使用vim編輯Podfile文件,添加Mantle和ReactiveObjC第三方庫:
# 這里Pod支持iOS系統(tǒng),還可以支持osx。如果沒有說明支持的系統(tǒng),默認(rèn)支持所有系統(tǒng)。
platform :ios, '9.0'
target 'MyApp' do
pod 'Mantle'
pod 'ReactiveObjC', '~> 3.1.0'
target 'MyAppTests' do
inherit! :search_paths
# Pods for testing
end
end
添加第三方庫時(shí),如果沒有指定版本,則默認(rèn)使用最新正式版。另外,使用如下命令指定版本:
// 使用2.1.0版本的Mantle
pod 'Mantle', '2.1.0'
除不使用版本號(hào)、固定版本號(hào),還可以使用邏輯符指定版本號(hào)。
-
'> 0.2':比0.2高的任何版本。 -
>= 0.2:大于等于0.2版本。 -
< 0.2:小于0.2的版本。 -
~> 0.6.48':大于等于0.6.48版本,但小于0.7版本。 -
'~> 0.6':大于等于0.6版本,但小于1.0版本。 -
'~> 1':主版本號(hào)大于等于1。
4.3 添加第三方庫
使用pod install命令添加第三方庫:
$ pod install
Analyzing dependencies
Downloading dependencies
Installing Mantle (2.1.0)
Installing ReactiveObjC (3.1.0)
Generating Pods project
Integrating client project
[!] Please close any current Xcode sessions and use `MyApp.xcworkspace` for this project from now on.
Sending stats
Pod installation complete! There are 2 dependencies from the Podfile and 2 total pods installed.
根據(jù)終端提示,如果想要使用剛添加的第三方庫,必須打開MyApp.xcworkspace,而非之前的MyApp.xcodeProj。
4.4 導(dǎo)入第三方庫
CocoaPods創(chuàng)建Mantlepod工程,并添加到你的workspace。因此,當(dāng)你導(dǎo)入Mantle時(shí),是從workspace的其它工程導(dǎo)入,需要使用尖括號(hào)(< >)而非雙引號(hào)(“ ”)。
#import <Mantle/Mantle.h>
5. pod install VS pod update
剛接觸CocoaPods時(shí),很多人會(huì)誤以為pod install用在初次設(shè)置工程時(shí),之后使用pod update,事實(shí)并非如此。
- 使用
pod install命令在project中添加新的pods。即使之前已經(jīng)使用過pod install命令,在添加、移除pods時(shí),依然使用pod install命令。 - 只有需要把pods更新到新版本時(shí),使用
pod update [PODNAME]命令。
5.1 pod install
第一次為project添加pods,編輯Podfile文件以添加、更新、移除pods,均使用pod install命令。
- 每次運(yùn)行
pod install命令時(shí),CocoaPods會(huì)把添加的pod版本信息寫入Podfile.lock文件,Podfile.lock文件跟蹤已安裝pod版本信息,并鎖定到當(dāng)前版本。 - 當(dāng)運(yùn)行
pod install命令時(shí),CocoaPods只處理未在Podfile.lock文件中列出的依賴項(xiàng)。- 對(duì)于
Podfile.lock文件中列出的pods,CocoaPods直接下載Podfile.lock文件中列出的版本,不會(huì)嘗試查找新版本。 - 對(duì)于未在
Podfile.lock文件中列出的pods,CocoaPods會(huì)根據(jù)Podfile文件添加pods。
- 對(duì)于
5.2 pod outdated
當(dāng)運(yùn)行pod outdated命令時(shí),CocoaPods會(huì)查找Podfile.lock中所有pods,并列出更新版本的pods。也就是說,運(yùn)行pod update [PODNAME]命令時(shí),如果新版本符合Podfile的描述,則會(huì)更新pod到新版本。
$ pod outdated
Updating spec repo `master`
$ /usr/local/bin/git -C /Users/ad/.cocoapods/repos/master fetch origin
--progress
remote: Counting objects: 45, done.
remote: Compressing objects: 100% (40/40), done.
remote: Total 45 (delta 27), reused 0 (delta 0), pack-reused 0
From https://github.com/CocoaPods/Specs
f7d947da03c..a14b55af97b master -> origin/master
$ /usr/local/bin/git -C /Users/ad/.cocoapods/repos/master rev-parse
--abbrev-ref HEAD
master
$ /usr/local/bin/git -C /Users/ad/.cocoapods/repos/master reset --hard
origin/master
HEAD is now at a14b55af97b [Add] Waterpurifier 1.0.4
CocoaPods 1.4.0.rc.1 is available.
To update use: `gem install cocoapods --pre`
[!] This is a test version we'd love you to try.
For more information, see https://blog.cocoapods.org and the CHANGELOG for this version at https://github.com/CocoaPods/CocoaPods/releases/tag/1.4.0.rc.1
Analyzing dependencies
The following pod updates are available:
- Mantle 1.4.1 -> 1.4.1 (latest version 2.1.0)
5.3 pod update
運(yùn)行pod update [PODNAME]命令時(shí),Cocoapods會(huì)查找[PODNAME]的新版本,如果新版本同時(shí)滿足Podfile 文件對(duì)版本描述,則會(huì)更新pod。
如果運(yùn)行pod update命令時(shí)不添加pod名稱,則會(huì)查找所有pods的新版本,并嘗試更新。
6. 查找第三方庫 pod search
Podfile文件中第三方庫名稱區(qū)分大小寫,寫錯(cuò)后install第三方庫時(shí)會(huì)提示語法錯(cuò)誤(syntax error)。為避免出錯(cuò),可以在添加之前先查找對(duì)應(yīng)庫。
在Terminal中輸入pod search [PODNAME]即可進(jìn)行查找,查找時(shí)不區(qū)分大小寫??梢耘浜掀渌蛇x項(xiàng)進(jìn)行檢索,如—simple只查找pod名稱,—stats顯示GitHub中watch fork數(shù)據(jù)。
$ pod search --simple --stats AFNetworking
-> AFNetworking (3.1.0)
A delightful iOS and OS X networking framework.
pod 'AFNetworking', '~> 3.1.0'
- Homepage: https://github.com/AFNetworking/AFNetworking
- Source: https://github.com/AFNetworking/AFNetworking.git
- Versions: 3.1.0, 3.0.4, 3.0.3, 3.0.2, 3.0.1, 3.0.0, 3.0.0-beta.3,
3.0.0-beta.2, 3.0.0-beta.1, 2.6.3, 2.6.2, 2.6.1, 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]
- Author: Mattt Thompson
- License: MIT
- Platform: iOS 7.0 - macOS 10.9 - tvOS 9.0 - watchOS 2.0
- Stars: 30058
- Forks: 9386
- Subspecs:
- AFNetworking/Serialization (3.1.0)
- AFNetworking/Security (3.1.0)
- AFNetworking/Reachability (3.1.0)
- AFNetworking/NSURLSession (3.1.0)
- AFNetworking/UIKit (3.1.0)
另外,也可以在CocoaPods官網(wǎng)進(jìn)行搜索。
總結(jié)
由于Swift不支持靜態(tài)庫(static library),在Swift中使用時(shí)必須使用framework。
target 'MyApp` do
use_frameworks!
pod 'MJRefresh`, '~> 3.1.15`
end
有時(shí)添加的第三庫會(huì)有一些警告信息,通過在Podfile添加以下命令可以隱藏第三方庫中的警告信息。
platform :ios, '9.0'
# 隱藏所有警告
inhibit_all_warnings!
target 'MyApp' do
pod 'Mantle', '1.4.1'
# 不要隱藏ReactiveObjC的警告,也可以用此方法只隱藏指定pod警告。
pod 'ReactiveObjC', '~> 3.1.0', :inhibit_warnings => false
end
另外,關(guān)于是否將Pods文件加入版本控制,可以根據(jù)下面的利弊自行決定:
- 將
Pods文件加入版本控制:- 可以在clone倉庫后立即開始工作,不需要
pod install。 - 可以保證每人都使用相同版本。
- 即使原始pod已經(jīng)移除也可以繼續(xù)使用。
- 可以在clone倉庫后立即開始工作,不需要
- 將
Pods文件排除在版本控制系統(tǒng)外:- 使得倉庫更小。
- 合并分支時(shí)不會(huì)出現(xiàn)不同Pod版本間沖突。
但需要注意的是,一定要把Podfile文件和Podfile.lock文件加入版本控制。
最后,CocoaPods除了將開源庫添加到你的項(xiàng)目外,還可以創(chuàng)建公開、私有pod,具體方法可以查看使用CocoaPods創(chuàng)建公開、私有pod這篇文章。