這篇文章,記錄一下CocoaPods的進階用法。
進階用法主要體現在.podspec文件和Podfile的配置上。
.podspec文件的進階配置
以官方的一個.podspec文件示例細說:
Pod::Spec.new do |spec| spec.name = 'Reachability' spec.version = '3.1.0' spec.license = { :type => 'BSD' } spec.homepage = 'https://github.com/tonymillion/Reachability' spec.authors = { 'Tony Million' => 'tonymillion@gmail.com' } spec.summary = 'ARC and GCD Compatible Reachability Class for iOS and OS X.' spec.source = { :git => 'https://github.com/tonymillion/Reachability.git', :tag => 'v3.1.0' } spec.module_name = 'Rich' spec.ios.deployment_target = '9.0' spec.osx.deployment_target = '10.10' spec.source_files = 'Reachability/common/*.swift' spec.ios.source_files = 'Reachability/ios/*.swift', 'Reachability/extensions/*.swift' spec.osx.source_files = 'Reachability/osx/*.swift' spec.framework = 'SystemConfiguration' spec.ios.framework = 'UIKit' spec.osx.framework = 'AppKit' spec.dependency 'SomeOtherPod' end
1.|spec|中的spec命名可以任意修改,只需要保持后續選項的對象名稱一致即可。
2.version字段對應Podfile文件配置中版本號,source的tag字段對應SCM源代碼的標簽
3.source字段指定了SCM源代碼地址和版本,支持如下關鍵字組:
:git => :tag, :branch, :commit, :submodules :svn => :folder, :tag, :revision :hg => :revision :http => :flatten, :type, :sha256, :sha1 :path
tag的寫法可以如下::tag => spec.version
建議如此,使得SCM源代碼的標簽和Podfile配置中版本號保持一致。
4.source_files字段,表明類庫包括的文件。
需要注意文件匹配的寫法:
*表示文件名通配符
**表示文件夾遞歸匹配
?表示一個任意字符的匹配
[a-z]表示匹配集合中的一個字符
{h,m}表示兩個字符中的任意一個
如下示例:
"JSONKit.?" #=> ["JSONKit.h", "JSONKit.m"] "*.[a-z][a-z]" #=> ["CHANGELOG.md", "README.md"] "*.[^m]*" #=> ["JSONKit.h"] "*.{h,m}" #=> ["JSONKit.h", "JSONKit.m"] "*" #=> ["CHANGELOG.md", "JSONKit.h", "JSONKit.m", "README.md"]
5.public_header_files和private_header_files分別表示公開和私有的頭文件路徑,便於集成時候自動配置引用路徑,默認公開全部頭文件
6.vendored_frameworks和verdored_libraries分別表示當前類庫中存在的.framework和.a文件的路徑,用於集成時候自動配置引用路徑
7.resource_bundles字段,表明編譯類庫需要的資源bundles及其文件,如下:
spec.resource_bundles = { 'MapBox' => ['MapView/Map/Resources/*.png'], 'OtherResources' => ['MapView/Map/OtherResources/*.png'] }
key表示bundle的名稱,value表示內部資源文件。
不建議使用類似字段resources,可能造成資源名字沖突,並且會將指定資源直接拷貝到目標target中。
8.exclude_files字段,表明不包括的文件
9.frameworks和libaries字段表明類庫引用的系統frameworks和libraries
需要注意的是,libraries的value需要省略前綴"lib",並且不需要擴展名。例如需要引用libz.dylib,則如下寫法:
s.libraries = 'z'
10.compiler_flags字段表明編譯flags,例如-Objc
11.pod_target_xcconfig字段表明任意編譯flag,如下:
spec.pod_target_xcconfig = { 'OTHER_LDFLAGS' => '-lObjC' }
12.header_dir字段表明存儲公開頭文件的文件夾名稱
13.dependency字段,表明依賴的其他類庫及版本,多個類庫分開書寫
14.requires_arc字段,表明是否啟用ARC,默認為true,也可以指定部分目錄或文件啟用ARC,如下:
spec.requires_arc = 'Classes/Arc' spec.requires_arc = ['Classes/*ARC.m', 'Classes/ARC.mm']
如果大多數文件啟用ARC,個人文件不啟用ARC,可以使用subspec字段:
Pod::Spec.new do |s|
... s.exclude_files = 'xxxx'
s.subspec 'Core' do |sp| sp.requires_arc = false
sp.source_files = 'xxxx' end s.subspec 'ObjectMapping' do |os| end end
15.subspec字段,定義一個子模塊,將部分文件移入其中,可添加局部配置,'Core'和'ObjectMapping'就是子模塊文件夾的名稱
Podfile文件的進階配置
1.pod指令,指定依賴庫時候,無版本號表示最新版本,具體版本號將鎖定版本,使用運算符可指定一個區間的版本;~> 0.1.2表示大於等於0.1.2並且小於0.2.0,此運算符對所給版本號最后一個部分有效。
2.如果需要使用本地倉庫,可以如下配置:
pod 'AFNetworking', :path => '~/Documents/AFNetworking'
但需要注意,所給路徑下應該存在.podspec文件
3.如果podspec文件存在於git倉庫中,即使沒有最新提交對應的podspec文件,也可用下列方式直接集成對應位置的類庫:
To use the master branch of the repository: pod 'AFNetworking', :git => 'https://github.com/gowalla/AFNetworking.git'
To use a different branch of the repository: pod 'AFNetworking', :git => 'https://github.com/gowalla/AFNetworking.git', :branch => 'dev'
To use a tag of the repository: pod 'AFNetworking', :git => 'https://github.com/gowalla/AFNetworking.git', :tag => '0.7.0'
Or specify a commit: pod 'AFNetworking', :git => 'https://github.com/gowalla/AFNetworking.git', :commit => '082f8319af'
特別是指定一個commit的方式,適合未發布標簽版本的代碼內測。
4.如果podspec文件不存在於git倉庫中,需要用如下方式訪問:
pod 'JSONKit', :podspec => 'https://example.com/JSONKit.podspec'
5.如果想消除全部依賴類庫的編譯提醒,可使用inhibit_all_warnings!,如下配置:
platform :ios, '9.0' inhibit_all_warnings! target 'MyApp' do pod 'ObjectiveSugar', '~> 0.5' ... end
也可以針對個別依賴庫設置編譯提醒的開關:
pod 'SSZipArchive', :inhibit_warnings => true pod 'SSZipArchive', :inhibit_warnings => false
n