CocoaPods 是開發 OS X 和 iOS 應用程序的一個第三方庫的依賴管理工具。利用 CocoaPods,能夠定義本身的依賴關係庫 (稱做 pods
),而且隨着時間的變化,在整個開發環境中對第三方庫的版本管理很是方便。html
在iOS項目開發過程當中,咱們常常會使用一些第三方庫,如AFNetworking、YYKit、Masonry等,經過這些優秀的第三方庫,來幫助咱們更有效率的進行開發。回想一下咱們導入這些第三方庫的過程:ios
上面的四個步驟,若是咱們使用CocoaPods,那麼只須要配置好相應的Podfile,CocoaPods會爲咱們去作好這些事情。git
CocoaPods是用Ruby 的依賴管理 gem 進行構建的,要想使用它首先須要有Ruby的環境。OS X系統默承認以運行Ruby,所以執行如下命令便可:github
$ sudo gem install cocoapods
安裝完成後,執行下面的指令,若是沒有報錯,則說明安裝成功。objective-c
$ pod setup
【說明】:若是執行上面指令的時候,長時間沒有反應,這多是由於Ruby的默認源使用cocoapods.org,國內訪問這個網址有時候會有問題,能夠將該源替換成淘寶的(若是淘寶的有問題,能夠用https://gems.ruby-china.org/),替換方式以下:swift
$ gem sources --remove https://rubygems.org/ $ gem sources -a http://ruby.taobao.org/
替換完成以後,執行指令:api
$ gem sources -l
若是輸出結果和下圖同樣,則表示替換成功。xcode
安裝過程當中,有可能遇到以下問題:ruby
問題一:does not match the server certificate,詳情以下:session
解決方案:該問題是由於使用都 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
CocoaPods的升級很簡單,直接執行安裝指令便可:
$ sudo gem install cocoapods
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'
爲了演示使用CocoaPods的過程,在這裏建立了一個MVVMDemo的演示項目,建立項目的過程這裏不細說了。
在終端進入項目所在目錄,而後用以下指令建立Podfile文件:
$ touch Podfile
此時項目文件夾裏會建立一個名爲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
編寫完成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命令執行成功了。如今再看一下工程目錄的變化:
從上圖能夠看到,多了三個文件:
打開MVVMDemo.xcworkspace工程,界面以下:
在項目中引用剛纔添加的第三方庫的頭文件,執行編譯操做,操做成功。
問題一:舊工程項目切換到CocoaPods,執行「pod install」指令時,有可能出現以下警告信息:
產生上面警告的緣由是項目 Target 中的一些設置,CocoaPods 也作了默認的設置,若是兩個設置結果不一致,就會形成問題。修改方案以下:
顯示包內容
,用文本編輯器打開project.pbxproj
,刪除OTHER_LDFLAGS
的地方,保存,執行pod update指令便可消除該警告;
關於$(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 + 動態連接庫 + 資源文件)
: