CocoaPods 是開發 OS X 和 iOS 應用程序的一個第三方庫的依賴管理工具。利用 CocoaPods,可以定義自己的依賴關系庫 (稱作 pods
),並且隨着時間的變化,在整個開發環境中對第三方庫的版本管理非常方便。
1.為什么要用CocoaPods?
在iOS項目開發過程中,我們經常會使用一些第三方庫,如AFNetworking、YYKit、Masonry等,通過這些優秀的第三方庫,來幫助我們更有效率的進行開發。回想一下我們導入這些第三方庫的過程:
- 第一步:下載第三方庫的源代碼並添加到工程;
- 第二步:添加第三方庫使用到的Framework;
- 第三步:處理第三方庫之間或第三方庫與工程之間的依賴關系以及重復添加等問題;
- 第四步:如果第三方庫有更新,需要將工程中使用的第三方庫刪除,重新執行前面的三個步驟。
上面的四個步驟,如果我們使用CocoaPods,那么只需要配置好相應的Podfile,CocoaPods會為我們去做好這些事情。
2.安裝CocoaPods
CocoaPods是用Ruby 的依賴管理 gem 進行構建的,要想使用它首先需要有Ruby的環境。OS X系統默認可以運行Ruby,因此執行以下命令即可:
$ sudo gem install cocoapods
安裝完成后,執行下面的指令,如果沒有報錯,則說明安裝成功。
$ pod setup
【說明】:如果執行上面指令的時候,長時間沒有反應,這可能是因為Ruby的默認源使用cocoapods.org,國內訪問這個網址有時候會有問題,可以將該源替換成淘寶的(如果淘寶的有問題,可以用https://gems.ruby-china.org/),替換方式如下:
$ gem sources --remove https://rubygems.org/ $ gem sources -a http://ruby.taobao.org/
替換完成之后,執行指令:
$ gem sources -l
如果輸出結果和下圖一樣,則表示替換成功。
安裝過程中,有可能遇到如下問題:
問題一:does not match the server certificate,詳情如下:
解決方案:該問題是因為使用都 https://gems.ruby-china.org/ 源路徑證書驗證未通過,替換成http://gems.ruby-china.org/即可。
問題二:Unable to resolve dependencies,詳情如下:
解決方案:gem版本比較低,升級即可。指令如下:
sudo gem update --system
問題三:Operation not permitted - /usr/bin/xcodeproj。
解決方案:沒有權限,執行如下指令安裝cocoapods:
sudo gem install -n /usr/local/bin cocoapods
問題四:怎么降低cocoapods版本?
//卸載當前版本 sudo gem uninstall cocoapods //安裝指定版本 sudo gem install -n /usr/local/bin cocoapods -v 1.2.1
3.升級CocoaPods
CocoaPods的升級很簡單,直接執行安裝指令即可:
$ sudo gem install cocoapods
4.Podfile文件說明
Podfile 是一個文件,用於定義項目所需要使用的第三方庫。該文件支持高度定制,詳細的信息可以參考Podfile 指南。下面列出常用的語法並進行說明。
先看一個示例的Podfile文件:
source 'https://github.com/Artsy/Specs.git' source 'https://github.com/CocoaPods/Specs.git' platform :ios, '8.0' inhibit_all_warnings! target 'MVVMReactiveCocoa' do pod 'SDWebImage', '~> 3.7.1' pod 'UIActivityIndicator-for-SDWebImage' pod 'MBProgressHUD', '~> 0.9' pod 'SSKeychain', '~> 1.2.2' pod 'IQKeyboardManager', '~> 3.2.0.3' pod 'SVPullToRefresh', '~> 0.4.1' pod 'MKNetworkKit', '~> 0.87' pod 'WebViewJavascriptBridge', '~> 4.1.4' pod 'FormatterKit', '~> 1.8.0' pod 'DZNEmptyDataSet', '~> 1.5.1' pod 'Ono', '~> 1.2.0' pod 'FMDB' pod 'UMengSocial', '~> 4.3' pod 'GPUImage', '~> 0.1.7' pod 'Reveal-iOS-SDK', '~> 1.6.0' pod 'Appirater' pod 'SDVersion' pod 'YYKit' pod 'OcticonsIOS', :git => 'https://github.com/jacksonh/OcticonsIOS.git', :commit => '4bd3b21' pod 'LCFInfiniteScrollView', :git => 'https://github.com/leichunfeng/LCFInfiniteScrollView.git' target 'MVVMReactiveCocoaTests' do inherit! :search_paths end end
看到上面的Podfile文件,有些語句的含義,我們也能大概理解,下面細說一下:
序號 | 語句 | 說明 |
1 | source 'URL' | 指定鏡像倉庫的源 |
2 | platform : iOS, '6.0' | 指定所支持系統和最低版本 |
3 | inhibit_all_warnings! | 屏蔽所有warning |
4 | workspace '項目空間名' | 指定項目空間名 |
5 | xcodeproj '工程文件名' | 指定xcodeproj工程文件名 |
6 | pod '庫名' | 引入庫,什么版本都可以(一般是最新版本) |
7 | pod '庫名', '版本' | 引入指定版本的庫 |
8 | pod '庫名', :podspec => 'podspec文件路徑' | 指定導入庫的podspec文件路徑 |
9 | pod '庫名', :Git => '源碼git地址' | 指定導入庫的源碼git地址 |
10 | pod '庫名', :tag => 'tag名' | 指定導入庫的Tag分支 |
關於引入庫的版本,除了指定和不指定之外,還有如下操作:
>0.1
高於0.1的任何版本;>=0.1
版本0.1和任何更高版本;<0.1
低於0.1的任何版本;<=0.1
版本0.1和任何較低的版本;〜>0.1.2
版本 0.1.2的版本到0.2 ,不包括0.2。這個基於你指定的版本號的最后一個部分。這個例子等效於>= 0.1.2並且 <0.2.0,並且始終是你指定范圍內的最新版本。
【補充】:關於pod指令,除了常規的pod '庫名'方式,還有下面這些帶參數的方式:
1.使用本地路徑。
pod 'AFNetworking', :path => '~/Documents/AFNetworking'
使用path方式,執行“pod install”,指令后,可以看到Pods下新加了一個目錄:
對比一下我們常規的pod方式:
2.使用主分支的版本,也就是默認寫法。
pod 'AFNetworking', :git => 'https://github.com/gowalla/AFNetworking.git'
3.不使用主分支,使用指定分支。
pod 'AFNetworking', :git => 'https://github.com/gowalla/AFNetworking.git', :branch => 'dev'
4.使用指定的commit版本。
pod 'AFNetworking', :git => 'https://github.com/gowalla/AFNetworking.git', :commit => '082f8319af'
5.使用指定tag版本
pod 'AFNetworking', :git => 'https://github.com/gowalla/AFNetworking.git', :tag => '0.7.0'
5.使用CocoaPods
5.1創建一個演示項目
為了演示使用CocoaPods的過程,在這里創建了一個MVVMDemo的演示項目,創建項目的過程這里不細說了。
5.2創建Podfile文件
在終端進入項目所在目錄,然后用如下指令創建Podfile文件:
$ touch Podfile
此時項目文件夾里會創建一個名為Podfile的文件,如下圖所示:
5.3編輯Podfile文件
使用XCode打開Podfile文件:
$ open -a Xcode Podfile
在這里,我們需要導入AFNetworking、YYKit、Masonry庫,因此在Podfile文件中輸入如下內容:
source 'https://github.com/CocoaPods/Specs.git' platform :ios, '8.0' inhibit_all_warnings!
target 'MVVMDemo' do pod 'AFNetworking' pod 'YYKit' pod 'Masonry'
end
5.4執行導入命令
編寫完成Podfile文件之后,保存關閉,輸入如下指令導入第三方庫:
$ pod install
CocoaPods就會做如下工作:下載源碼、配置依賴關系、引入需要的framework等。命令的執行結果如下所示:
Updating local specs repositories CocoaPods 1.1.0.beta.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 http://blog.cocoapods.org and the CHANGELOG for this version http://git.io/BaH8pQ. Analyzing dependencies Downloading dependencies Installing AFNetworking (3.1.0) Installing Masonry (1.0.1) Installing YYKit (1.0.7) Generating Pods project Integrating client project [!] Please close any current Xcode sessions and use `MVVMDemo.xcworkspace` for this project from now on. Sending stats Sending stats Pod installation complete! There are 3 dependencies from the Podfile and 3 total pods installed.
這說明pod install命令執行成功了。現在再看一下工程目錄的變化:
從上圖可以看到,多了三個文件:
- Podfile.lock:這是 CocoaPods 創建的最重要的文件之一。它記錄了需要被安裝的 pod 的每個已安裝的版本。如果你想知道已安裝的 pod 是哪個版本,可以查看這個文件。推薦將 Podfile.lock 文件加入到版本控制中,這有助於整個團隊的一致性。
- MVVMDemo.xcworkspace:從上面的執行結果可以看到,紅色部分的注釋提示我們現在項目用MVVMDemo.xcworkspace來打開,原來的工程設置已經被更改了,如果直接打開原來的工程文件去編譯就會報錯,只能使用新生成的workspace來進行項目管理。
- Pods:CocoaPods會將所有的第三方庫以target的方式組成一個名為Pods的工程。整個第三方庫工程會生成一個名稱為libPods.a的靜態庫給MVVMDemo項目使用。
打開MVVMDemo.xcworkspace工程,界面如下:
在項目中引用剛才添加的第三方庫的頭文件,執行編譯操作,操作成功。
6.常見問題
問題一:舊工程項目切換到CocoaPods,執行“pod install”指令時,有可能出現如下警告信息:
產生上面警告的原因是項目 Target 中的一些設置,CocoaPods 也做了默認的設置,如果兩個設置結果不一致,就會造成問題。修改方案如下:
- 警告“...target overrides the `OTHER_LDFLAGS` build setting defined...”:點擊項目文件 project.xcodeproj,右鍵
顯示包內容
,用文本編輯器打開project.pbxproj
,刪除OTHER_LDFLAGS
的地方,保存,執行pod update指令即可消除該警告;
- 警告“...target overrides the `GCC_PREPROCESSOR_DEFINITIONS` build setting defined...”:修改工程target,具體是這兩項“Build Settings -> Other linker flags”和“Build Settings -> Preprocessor Macros”,在這兩處添加“$(inherited)”;修改“Build Settings -> Preprocessor Macros”時,需要留意一下保留DEBUG時的日志打印(DEBUG=1);
- 警告“...target overrides the `LIBRARY_SEARCH_PATHS` build setting defined...” :修改工程target,具體是“Build Settings -> Library Search Paths”,在這里添加“$(inherited)”;
- 警告“...target overrides the `HEADER_SEARCH_PATHS` build setting defined...”:修改工程target,具體是“Build Settings -> Header Search Paths”,在這里添加“$(inherited)”;
關於$(inherited)可查看這兩篇文章:
問題二:怎樣在CocoaPods中使用私有庫?
很簡單,兩個步驟:
第一步:引入source源:
source 'git@git:/Data/git/ios/GofSpecs.git'
第二步:pod私有庫:
pod 'GofKit'
問題三:怎么加快pod install 或pod update指令執行時間?
執行pod install 或pod update 很多時候都卡在了Analyzing dependencies不動,這是更新本地的pod spec索引文件導致的。通過--no-repo-update標志可以不更新本地pod spec索引。當然首次install不應該添加這個標志,后續修改Podfile的時候可以適當使用,加快pod速度。
問題四:怎樣輸出指令詳細日志?
pod install --verbose
問題五:怎樣忽略某些文件或文件夾,讓這些文件或文件夾不使用git管理?
在項目的根目錄(跟.git文件夾所在目錄同層)建立.gitignore文件,在里面聲明即可。例如:
#ignore these files GofViewMakerDemo/Pods/*
上面的.gitignore文件意思就是“GofViewMakerDemo/Pods/*”目錄下的所有文件不使用git管理。
這里有一份可忽略的文件列表:

# Created by https://www.gitignore.io/api/objective-c,swift ### Objective-C ### # Xcode # # gitignore contributors: remember to update Global/Xcode.gitignore, Objective-C.gitignore & Swift.gitignore ## Build generated build/ DerivedData/ ## Various settings *.pbxuser !default.pbxuser *.mode1v3 !default.mode1v3 *.mode2v3 !default.mode2v3 *.perspectivev3 !default.perspectivev3 xcuserdata/ ## Other *.moved-aside *.xcuserstate ## Obj-C/Swift specific *.hmap *.ipa # CocoaPods # # We recommend against adding the Pods directory to your .gitignore. However # you should judge for yourself, the pros and cons are mentioned at: # https://guides.cocoapods.org/using/using-cocoapods.html#should-i-check-the-pods-directory-into-source-control # # Pods/ # Carthage # # Add this line if you want to avoid checking in source code from Carthage dependencies. # Carthage/Checkouts Carthage/Build # fastlane # # It is recommended to not store the screenshots in the git repo. Instead, use fastlane to re-generate the # screenshots whenever they are needed. # For more information about the recommended setup visit: # https://github.com/fastlane/fastlane/blob/master/fastlane/docs/Gitignore.md fastlane/report.xml fastlane/screenshots ### Objective-C Patch ### *.xcscmblueprint ### Swift ### # Xcode # # gitignore contributors: remember to update Global/Xcode.gitignore, Objective-C.gitignore & Swift.gitignore ## Build generated build/ DerivedData/ ## Various settings *.pbxuser !default.pbxuser *.mode1v3 !default.mode1v3 *.mode2v3 !default.mode2v3 *.perspectivev3 !default.perspectivev3 xcuserdata/ ## Other *.moved-aside *.xcuserstate ## Obj-C/Swift specific *.hmap *.ipa ## Playgrounds timeline.xctimeline playground.xcworkspace # Swift Package Manager # # Add this line if you want to avoid checking in source code from Swift Package Manager dependencies. # Packages/ .build/ # CocoaPods # # We recommend against adding the Pods directory to your .gitignore. However # you should judge for yourself, the pros and cons are mentioned at: # https://guides.cocoapods.org/using/using-cocoapods.html#should-i-check-the-pods-directory-into-source-control # # Pods/ # Carthage # # Add this line if you want to avoid checking in source code from Carthage dependencies. # Carthage/Checkouts Carthage/Build # fastlane # # It is recommended to not store the screenshots in the git repo. Instead, use fastlane to re-generate the # screenshots whenever they are needed. # For more information about the recommended setup visit: # https://github.com/fastlane/fastlane/blob/master/fastlane/docs/Gitignore.md fastlane/report.xml fastlane/Preview.html fastlane/screenshots fastlane/test_output
問題六:怎樣導入swift庫?
platform :ios, '8.0' use_frameworks! pod 'Alamofire', '~> 1.3'
關於Library 和 Framework的詳細內容,可以參看這幾篇文章:
這里做一個簡單的介紹:
用cocoapods 導入swift 框架 到 swift項目和OC項目都必須要 use_frameworks!
use_frameworks!只能在iOS 8及以上平台使用;
使用 dynamic frameworks,必須要在Podfile文件中添加 use_frameworks!
不使用use_frameworks!,在執行pod update指令之后,會生成對應的.a文件(靜態鏈接庫)
:
使用use_frameworks!,在執行pod update指令之后,會生成對應的.frameworks文件(動態鏈接庫:實際內容為 Header + 動態鏈接庫 + 資源文件)
:
7.參考資料