您的位置:首页 > 其它

(四)Podfile文件(原文翻译)

2017-01-22 18:06 232 查看
学习关于Podfile的一切。Podfile用于在工程中声明依赖。

1 Podfile是什么?

Podfile是描述一个或多个Xcode工程的目标的依赖的明确说明。这个文件被简单命名为Podfile。本指南中的所有例子都是基于CocoaPods1.0及以前版本的。

Podfile文件可以非常简单,这里就是添加Alamofire到一个单独目标的代码:

target 'MyApp' do
use_frameworks!
pod 'Alamofire', '~> 3.0'
end


更复杂的Podfile的例子是链接app和它的测试包:

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

我们有一个博客帖子深入的解释了这个变化。

1.2 指定pod版本

当开始一个工程时,可能你想要使用某个Pod库的最新版本。在这种情况下,简单的删除版本要求即可。

pod 'SSZipArchive'


之后在这个工程里你可能想要冻结这个Pod的指定版本,在这种情况下你可以指定版本号。

pod 'Objection', '0.9'


除了不指定版本,或者指定一个版本,也可以使用逻辑运算符:

‘> 0.1’ Any version higher than 0.1 任何比0.1大的版本

‘>= 0.1’ Version 0.1 and any higher version 任何大于等于0.1的版本

‘< 0.1’ Any version lower than 0.1 任何比0.1小的版本

‘<= 0.1’ Version 0.1 and any lower version 任何小于等于0.1的版本

In addition to the logic operators CocoaPods has an optimistic operator ~>:

在逻辑运算符之外,CocoaPods还有乐观运算符~>:

‘~> 0.1.2’ Version 0.1.2 and the versions up to 0.2, not including 0.2 and higher 大于等于0.1.2且小于0.2的版本,不包含0.2及更高版本

‘~> 0.1’ Version 0.1 and the versions up to 1.0, not including 1.0 and higher 大于等于0.1且小于1.0的版本,不包含1.0及更高版本

‘~> 0’ Version 0 and higher, this is basically the same as not having it. 版本0及以上版本,这与不写基本没区别

For more information, regarding versioning policy, see:

关于版本策略的更多信息,请参考:

Semantic Versioning

RubyGems Versioning Policies

There’s a great video from Google about how this works: “CocoaPods and the Case of the Squiggly Arrow (Route 85)”.

2 从本地设备的文件夹路径使用文件

如果你想要联合开发一个Pod库及它的客户端工程,你可以使用:path。

pod 'Alamofire', :path => '~/Documents/Alamofire'


使用这个选项时,CocoaPods会假定给出的文件夹是Pod库的根节点,并且直接从那儿连接文件到Pods工程。这意味着你的编辑会在CocoaPods的安装中保持。引用的文件夹可以是从你最喜欢的SCM检出,或者甚至是当前repo(仓库)的git子模块。

注意Pod文件的podspec应该在指定的文件夹里。

2.1 从库repo的根节点里的podspec开始

有时你可能想使用Pod库的最前沿版本、指定的修正版或者你自己的分支。在这种情况下,你可以指定你的pod声明。

要使用repo的主干分支:

pod 'Alamofire', :git => 'https://github.com/Alamofire/Alamofire.git'


要使用repo的不同分支:

pod 'Alamofire', :git => 'https://github.com/Alamofire/Alamofire.git', :branch => 'dev'


要使用repo的tag标签:

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的版本要满足来自于其他Pods的任何其他依赖。

podspec文件应该在repo的根节点,如果这个库在它的repo中没有podspec文件,你将不得不使用后面章节中介绍的方法之一。

3 扩展资源

Non-trivial Podfile in Artsy/Eigen

Podfile for a Swift project in Artsy/Eidolon

原文链接:《The Podfile
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  xcode CocoaPods