[TOC]
# 1. 什么是 Podfile?
Podfile 是一個規范,用于描述一個或多個 Xcode 項目的目標依賴關系。 該文件應該簡單地命名為 Podfile 。 指南中的所有示例均基于 CocoaPods 1.0 及更高版本。
Podfile 可以非常簡單,例如將 Alamofire 添加到單個目標中:
~~~
target 'MyApp' do
use_frameworks!
pod 'Alamofire', '~> 3.0'
end
~~~
一個鏈接到應用程序及其測試包的更復雜的 Podfile 的示例:
~~~
source 'https://github.com/CocoaPods/Specs.git'
source 'https://github.com/Artsy/Specs.git'
platform :ios, '9.0'
inhibit_all_warnings!
target 'MyApp' do
pod 'GoogleAnalytics', '~> 3.1'
# Has its own copy of OCMock
# and has access to GoogleAnalytics via the app
# that hosts the test target
target 'MyAppTests' do
inherit! :search_paths
pod 'OCMock', '~> 2.0.1'
end
end
post_install do |installer|
installer.pods_project.targets.each do |target|
puts target.name
end
end
~~~
如果您希望多個目標共享相同的 pods,請使用 `abstract_target`。
~~~
# There are no targets called "Shows" in any Xcode projects
abstract_target 'Shows' do
pod 'ShowsKit'
pod 'Fabric'
# Has its own copy of ShowsKit + ShowWebAuth
target 'ShowsiOS' do
pod 'ShowWebAuth'
end
# Has its own copy of ShowsKit + ShowTVAuth
target 'ShowsTV' do
pod 'ShowTVAuth'
end
end
~~~
在 Podfile 的根部有隱含的抽象目標,所以你可以將上面的例子寫成:
~~~
pod 'ShowsKit'
pod 'Fabric'
# Has its own copy of ShowsKit + ShowWebAuth
target 'ShowsiOS' do
pod 'ShowWebAuth'
end
# Has its own copy of ShowsKit + ShowTVAuth
target 'ShowsTV' do
pod 'ShowTVAuth'
end
~~~
## 1.1 從 0.x 遷移到 1.0
我們有一篇[博客文章](http://blog.cocoapods.org/CocoaPods-1.0/)解釋深度變化。
## 1.2 指定 pod 版本
從項目開始時,您很可能會想要使用最新版本的Pod。 如果是這種情況,只需省略版本要求即可。
~~~
pod 'SSZipArchive'
~~~
在稍后的項目中,您可能想要凍結到特定版本的 Pod,在這種情況下,您可以指定該版本號。
~~~
pod 'Objection', '0.9'
~~~
除了沒有版本或特定版本,還可以使用邏輯運算符:
* `'> 0.1'` 任何高于 0.1 的版本
* `'> = 0.1'` 版本 0.1 和任何更高版本
* `'<0.1'` 低于 0.1 的任何版本
* `'<= 0.1'` 版本 0.1 和任何更低的版本
除了邏輯運算符 CocoaPods 還有一個樂觀的運算符 `?>` :
* `'?> 0.1.2'` 版本 0.1.2 和版本高達 0.2,不包括 0.2 和更高版本
* `'?> 0.1'` 版本 0.1 和版本高達 1.0,不包括 1.0 和更高版本
* `'?> 0'` 版本 0 和更高版本,這基本上與沒有它相同。
有關版本控制策略的更多信息,請參閱:
* [語義版本](http://semver.org/)
* [RubyGems 版本控制策略](http://guides.rubygems.org/patterns/#semantic-versioning)
* 谷歌有一個關于它如何工作的偉大視頻:["CocoaPods and the Case of the Squiggly Arrow (Route 85)"](https://www.youtube.com/watch?v=x4ARXyovvPc)
# 2. 使用機器本地文件夾中的文件
如果你想開發Pod與其客戶端項目,你可以使用` :path`。
~~~
pod 'Alamofire', :path => '~/Documents/Alamofire'
~~~
使用此選項,CocoaPods將假定給定的文件夾是Pod的根目錄,并將直接從Pods項目中的文件鏈接文件。 這意味著您的編輯將在CocoaPods安裝之間持續存在。 引用的文件夾可以是您最喜歡的SCM的 checkout,甚至可以是當前倉庫的git子模塊。
> 請注意,Pod 文件的 podspec 應該位于指定的文件夾中。
## 2.1 資源倉庫的根目錄中的 podspec
有時你可能想要使用 Pod 的最新版本,特定版本或你自己的分支。 如果是這種情況,您可以使用您的 pod 聲明來指定。
要使用倉庫的主分支:
~~~
pod 'Alamofire', :git => 'https://github.com/Alamofire/Alamofire.git'
~~~
要使用repo的不同分支:
~~~
pod 'Alamofire', :git => 'https://github.com/Alamofire/Alamofire.git', :branch => 'dev'
~~~
要使用倉庫標簽:
~~~
pod 'Alamofire', :git => 'https://github.com/Alamofire/Alamofire.git', :tag => '3.1.1'
~~~
或者指定一個提交:
~~~
pod 'Alamofire', :git => 'https://github.com/Alamofire/Alamofire.git', :commit => '0f506b1c45'
~~~
但是,需要注意的是,這意味著該版本將不得不滿足其他 Pod 的其他依賴項。
預計 podspec 文件位于 repo 的根目錄中,如果此庫的 repo 中沒有 podspec 文件,則必須使用下面各節中概述的方法之一。
# 3. 外部資源
* [Non-trivial Podfile in Artsy/Eigen](https://github.com/artsy/eigen/blob/master/Podfile)
* [Podfile for a Swift project in Artsy/Eidolon](https://github.com/artsy/eidolon/blob/master/Podfile)