CocoaPods中Podfile详解
2016-04-22 10:49
399 查看
1、Podfile简介
Podfile的官方说明为:The Podfile is a specification that describes the dependencies of the targets of one or more Xcode projects.即Podfile文件用来配置第三方库与一或多个Xcode项目之间的依赖信息。复杂的Podfile举例如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | platform :ios, '9.0' inhibit_all_warnings!//在全局指定不显示所有所引用的库中的警告信息 target 'MyApp' do pod 'ObjectiveSugar', '~> 0.5' target "MyAppTests" do inherit! :search_paths pod 'OCMock', '~> 2.0.1' end end post_install do |installer|//指定"安装完成,但是生成的工程还没有写入磁盘"时要执行的操作 installer.pods_project.pod_targets.each do |target| puts "#{target.name}" end end |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 | platform :ios, '8.0' use_frameworks!//通过指定use_frameworks!要求生成的是framework而不是静态库 inhibit_all_warnings!//在全局指定不显示所有所引用的库中的警告信息 def shared_pods//定义依赖宏 pod 'Alamofire', '~> 3.0' pod 'Kingfisher', '~> 1.6' pod 'MJRefresh' pod 'SDCycleScrollView','~> 1.3' pod 'APParallaxHeader' pod 'RoundImageView', '~> 1.0.1' pod 'StrechyParallaxScrollView', '~> 0.1' pod 'TextFieldEffects' pod 'IQKeyboardManagerSwift' pod 'SwiftyJSON' pod 'Validator' pod 'Qiniu', '~> 7.0' pod 'Google-Mobile-Ads-SDK', '~> 7.0' end target 'ds_ios' do shared_pods//使用宏 end |
2、Target configuration
这里的配置会使用和控制工程的生成。
2.1、platform
1 2 3 4 | platform :ios, '7.0' platform :ios |
iOS:4.3
OS X:10.6
tvOS:9.0
watchOS:2.0
2.2、project
默认情况下是没有指定的,当没有指定时,会使用Podfile目录下与target同名的工程:1 2 3 4 5 6 7 8 9 10 11 12 13 | # MyGPSApp只有在FastGPS工程中才会链接 target 'MyGPSApp' do project 'FastGPS' ... end # MyNotesApp这个target只有在FastNotes工程中才会链接 target 'MyNotesApp' do project 'FastNotes' ... end |
1 2 3 4 5 | target 'MyApp' do pod ... end |
4.3、inhibit_all_warnings!
inhibit_all_warnings!命令是不显示所引用的库中的警告信息。我们可以指定全局不显示警告信息,也可以指定某一个库不显示警告信息:1 2 3 | pod 'SSZipArchive', :inhibit_warnings => true//指定不显示某个库的警告信息 |
4.4、use_frameworks!
通过指定use_frameworks!要求生成的是framework而不是静态库。
4.5、workspace
默认情况下,我们不需要指定workspace,而是直接使用与Podfile所在目录的工程名一样的workspace就可以了。如果要指定另外的名称,而不是使用工程的名称,可以这样指定:1 2 3 | workspace 'MyWorkspace' |
4.6、source
source是指定pod的来源。如果不指定source,默认是使用CocoaPods官方的source。通常我们没有必要添加。1 2 3 4 5 6 7 | // 如果不想使用官方的,而是在别的地方也有,可以这样指定 source 'https://github.com/artsy/Specs.git' // 默认是官方的source source 'https://github.com/CocoaPods/Specs.git' |
4.7、Hooks
Hooks可以叫它为勾子吧,与swizzling特性差不多,就是在某些操作之前,先勾起,而且让它执行我们特定的操作。
4.8、plugin
Specifies the plugins that should be used during installation.Use this method to specify a plugin that should be used during installation, along with the options that should be passed to the plugin when it is invoked.例如,指定在安装期间使用cocoapods-keys和slather这两个插件:
1 2 3 4 | plugin 'cocoapods-keys', :keyring => 'Eidolon' plugin 'slather' |
4.9、pre_install
This hook allows you to make any changes to the Pods after they have been downloaded but before they are installed.当我们下载完成,但是还没有安装之时,会勾起来,然后可以通过pre_install指定要做的事,做完后才进入安装阶段。比如:在下载完成但未安装之前,我们就可以指定在干些什么:
1 2 3 4 5 | pre_install do |installer| # Do something fancy! end |
4.10、post_install
This hook allows you to make any last changes to the generated Xcode project before it is written to disk, or any other tasks you might want to perform.当我们安装完成,但是生成的工程还没有写入磁盘之时,我们可以指定要执行的操作。比如,我们可以在写入磁盘之前,修改一些工程的配置:
1 2 3 4 5 6 7 8 9 | post_install do |installer| installer.pods_project.targets.each do |target| target.build_configurations.each do |config| config.build_settings['GCC_ENABLE_OBJC_GC'] = 'supported' end end end |
4.11、def
我们还可以通过def命令来声明一个pod集:1 2 3 4 5 | def 'CustomPods' pod 'IQKeyboardManagerSwift' end |
1 2 3 4 5 | target 'MyTarget' do CustomPods end |
3、Podfile全局配置
目前根据官方文档说明,Podfile全局配置只有一个命令:1 2 3 | install! |
例如:
1 2 3 4 5 | install! 'cocoapods', :deterministic_uuids => false, :integrate_targets => false |
1 2 3 4 5 6 7 8 9 10 11 | :clean :deduplicate_targets :deterministic_uuids :integrate_targets :lock_pod_sources |
在我们日常开发中,我们可能永远不需要使用到此配置命令,因此大家不用太关注它!
4、Dependencies(依赖)
CocoaPods就是用于管理第三方依赖的。我们通过Podfile文件来指定工程中的每个target之间与第三方之间的依赖。有以下三个命令来管理依赖:
pod 指定特定依赖。比如指定依赖AFNetwroking
podspec 提供简单的API来创建podspec
target 通过target指定工程所依赖的范围。
4.1、Pod命令
pod命令用于指定工程所依赖的第三方及第三方库的版本范围。
4.1.1、永远使用最新版本
1 2 3 | pod 'HYBMasonryAutoCellHeight' |
4.1.2、使用固定版本
1 2 3 | pod 'HYBLoopScrollView', '2.0' |
4.1.3、指定版本范围
1 2 3 | pod 'HYBUnicodeReadable', '~>1.1.0' |
bug,当有bug被fix时,确实应该更新。从1.9.9升级到2.0.0时,不会去更新到2.0.0版本。我们认为从2.0.0是一个大版本,大版本的发布,通常不是fix bug,而是增加功能或者改动较大。
pod中用于指定范围的符号如下:
>= version 要求版本大于或者等于version,当有新版本时,都会更新至最新版本
< version 要求版本小于version,当超过version版本后,都不会再更新
<= version 要求版本小于或者等于version,当超过version版本后,都不会再更新
~> version 比如上面说明的version=1.1.0时,范围在[1.1.0, 2.0.0)。注意2.0.0是开区间,也就是不包括2.0.0。
4.1.4、使用本地库
1 2 3 | pod 'AFNetworking', :path => '~/Documents/AFNetworking' |
[title2]
4.2、podspec(=注意由于我并没有向CocoaPods上传过开源库,所以对podspec理解的不清楚)[/title2]
官方说明为:Use just the dependencies of a Pod defined in the given podspec file. If no arguments are passed the first podspec in the root of the Podfile is used. It is intended to be used by the project of a library. Note: this does not include the sources derived
from the podspec just the CocoaPods infrastructure.大概意思是:使用给定的podspec所指定的pod依赖。如果没有指定参数,则使用根目录下的podspec。正常情况下,我们并不需要指定参数,一般所开源出来的库的podspec都是在根目录下,所以可放心地使用,不用考虑太多。
例如:
1 2 3 4 5 6 7 8 9 10 | // 不指定参数表示使用根目录下的podspec,默认开源库的podspec一般都会放在根目录下 podspec // 如果podspec的名字与库名不一样,可以通过这样来指定 podspec :name => 'QuickDialog' // 如果podspec不是在根目录下,那么可以通过:path来指定路径 podspec :path => '/Documents/PrettyKit/PrettyKit.podspec' |
4.2.1、通过仓库的podspec引入
Sometimes you may want to use the bleeding edge version of a Pod. Or a specific revision. If this is the case, you can specify that with your pod declaration.当我们需要使用库的混合边缘版本,或者指定的修订版本,我们可以通过指定像下面这样的声明。
使用仓库的master(主干):
1 2 3 | pod 'AFNetworking', :git => 'https://github.com/gowalla/AFNetworking.git' |
1 2 3 | pod 'AFNetworking', :git => 'https://github.com/gowalla/AFNetworking.git', :branch => 'dev' |
1 2 3 | pod 'AFNetworking', :git => 'https://github.com/gowalla/AFNetworking.git', :tag => '0.7.0' |
1 2 3 | pod 'AFNetworking', :git => 'https://github.com/gowalla/AFNetworking.git', :commit => '082f8319af' |
4.2.2、从外部podspec引入
1 2 3 | pod 'JSONKit', :podspec => 'https://example.com/JSONKit.podspec' |
4.3、target
Defines a CocoaPods target and scopes dependencies defined within the given block. A target should correspond to an Xcode target. By default the target includes the dependencies defined outside of the block, unless instructed not to inherit! them.大概意思是:在给定的块内定义target独有的依赖,target与Xcode工程的target关联。默认情况下,target会包含定义在块外的依赖,除非指定不使用inherit!来继承(说的是嵌套的块里的继承问题)例子:
我们指定HYBTestProject这个target可以访问HYBMasonryAutoCellHeight库:
1 2 3 4 5 | target 'HYBTestProject' do pod 'HYBMasonryAutoCellHeight', '~>1.1.0' end |
1 2 3 4 5 6 7 8 9 10 | target 'HYBTestProject' do pod 'SSZipArchive' target 'HYBTestProjectTests' do inherit! :search_paths pod 'Nimble' end end |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | target 'ShowsApp' do pod 'ShowsKit' # 可以访问ShowsKit + ShowTVAuth,其中ShowsKit是继承于父层的 target 'ShowsTV' do pod 'ShowTVAuth' end # 可以访问Specta + Expecta # 同时也可以访问ShowsKit,它是明确指定继承于父层的所有pod target 'ShowsTests' do inherit! :search_paths pod 'Specta' pod 'Expecta' end end |
相关文章推荐
- mac 安装 Android Studio
- 【转】DBMS_PARALLEL_EXECUTE
- vs+opencv中findcontours崩溃的问题
- Git学习(2)Git 安装
- Android Paint详解
- C++作业4
- Android程序开发之UIScrollerView里有两个tableView
- 一个很city的问题
- c++第三次作业
- cocos中使用tinyxml2遇到的一个小问题
- 二维数组赋值的问题
- Oracle创建用户、表(1)
- Set Matrix Zeroes
- Oracle创建用户、表(1)
- 移动金融应用面临的风险及应对
- adb 开启和关闭wifi
- 微信web开发者工具
- 笔试——JAVA相关
- 解决在启动eclipse的tomcat进行访问时出现404问题的方法
- C++第四次上机实验作业