2.podfile文件講解
podfile是一個規(guī)范文件,描述一個或多個項目目標依賴項,CocoaPods管理iOS組件庫
各種參數(shù)配置含義
一、Install! :適用于整個podfile文件
-
指明了cocoapods安裝podfile使用的安裝方法和配置項
install! 'cocoapods', //第一個參數(shù)是安裝方法,剩下的參數(shù)是選擇項,現(xiàn)在安裝方法只支持’cocoapods’ :deterministic_uuids => false, :integrate_targets => false -
還有以下配置參數(shù)
:clean :deduplicate_targets :deterministic_uuids :integrate_targets :lock_pod_sources :warn_for_multiple_pod_sources :share_schemes_for_development_pods正常情況下 不需要配置這個參數(shù)
-
build configurations (編譯配置) 默認情況下,依賴項會被安裝在所有target的build configrations中。
//為了調試或者其他原因,他們可以在給定的configurations中啟用 pod 'PonyDebugger', :configurations => ['Debug', 'Beta'] //或者,你可以至指定一個build configration pod 'PonyDebugger', :configuration => ‘Debug'注意:默認情況下依賴會包含在所有配置中,如果你想指定需要手動配置。
-
source: 默認被指定的依賴項會在全局級別的指定源中匹配搜索??梢詾樘匾蕾囮P系指定源
//指定特定源中搜索,并忽略任何全局源* pod 'PonyDebugger', :source => 'https://github.com/CocoaPods/Specs.git' -
Subspecs: ##### 當使用依賴庫名字引入依賴庫時,也會默認安裝依賴庫中的所有子模塊。
//指定引用指定子模塊 pod 'QueryKit/Attribute’ //指定一個子模塊集合 pod 'QueryKit', :subspecs => ['Attribute', 'QuerySet']
二、依賴(Dependencies)
-
pod: 指明項目依賴,一個依賴是由一個pod名稱和一個可選版本定義
a. 如果不添加版本號,pod默認使用最新的 如:pod ’SSZipArchive’
b. 如果項目需要一個指定的pod,需要添加版本號,如: pod ‘objection’, ‘0.9’
c. 指定版本范圍· = 0.1 版本是0.1
· >0.1 任何大于0.1版本
· >=0.1 0.1和大于0.1版本
· <0.1 小于0.1版本
· <=0.1 0.1和小于0.1版本
· ~=0.1.2 0.1.2<= pod < 0.2 版本 ,安裝這個范圍內最新的版本
-
podspec : 引用倉庫根目錄的(from a pod spec in the root of a library repository)引用pod在指定節(jié)點或者分支
//主分支: pod 'AFNetworking', :git => 'https://github.com/gowalla/AFNetworking.git' //指定分支: :branch => 'dev' pod 'AFNetworking', :git => 'https://github.com/gowalla/AFNetworking.git', :branch => 'dev' //指定的tag: :tag => '0.7.0' pod 'AFNetworking', :git => 'https://github.com/gowalla/AFNetworking.git', :tag => '0.7.0' //指定的節(jié)點: :commit => '082f8319af' pod 'AFNetworking', :git => 'https://github.com/gowalla/AFNetworking.git', :commit => ‘082f8319af' -
target: 在制定的快內定義pod target和指定的依賴范圍嗎。target應該與Xcode的target相符。默認情況下,target包含定義在塊外的依賴,除非指定不使用inherit!來繼承。例如:
//a. target A 引入AFN庫 target ‘A'do pod 'AFN' end //b.定義’A’ 引入AFN庫,定義’B’target引入’SSZip’庫,同時會繼承’A’target 中的’AFN’庫 target ‘A’ do pod ‘AFN’ target ‘B’ do inherit! :search_paths pod 'SSZip' end end // c. Target 多層嵌套 target 'ShowsApp’ do # ShowsApp 僅僅引入ShowsKit pod 'ShowsKit’ # 引入 ShowsKit 和 ShowTVAuth target 'ShowsTV' do pod 'ShowTVAuth’ end # 引入了Specta和Expecta以及ShowsKit target 'ShowsTests' do inherit! :search_paths pod 'Specta’ pod 'Expecta’ end end end -
abstract_target :定義一個抽象的target,為了方便target目標依賴繼承。這個target是沒有被定義在xcode中的。例子:
// a.定義一個抽象target abstract_target 'Networking' do pod ‘AlamoFire' target 'Networking App 1’ target 'Networking App 2’ end // b. 定義一個包含多個target的抽象target # 注意:這是個抽象的target工程中并沒有這個target.引入ShowsKit abstract_target 'Shows' do pod 'ShowsKit’ # ShowsiOS target會引入ShowWebAuth庫以及繼承自Shows的ShowsKit庫 target 'ShowsiOS' do pod 'ShowWebAuth’ end # ShowsTV target會引入ShowTVAuth庫以及繼承自Shows的ShowsKit庫 target 'ShowsTV’ do pod ‘ShowTVAuth' end # ShowsTests target引入了Specta和Expecta庫,并且指明繼承Shows,所以也會引入ShowsKit target 'ShowsTests’ do inherit! :search_paths pod 'Specta’ pod 'Expecta’ end end意義:可以使用抽象類 來統(tǒng)一 需要繼承的pod。
-
script_phase 使用這個命令給target添加shell腳本
target ‘A’ do script_phase :name => 'HelloWorldScript', :script => 'echo "Hello World”' script_phase :name => 'HelloWorldScript', :script => 'puts "Hello World"', :shell_path => '/usr/bin/ruby' end[圖片上傳失敗...(image-ba78ef-1536051559429)]
-
abstract! 指定當前target是抽象target
target ‘A’ do abstract! end -
inherit! 設置當前target的繼承關系
target 'App’ do target ‘A’ do #這個target 繼承 父級所有行為 inherit! :complete end target ‘B’ do #這個target 不繼承 父級所有行為 inherit! :none end target ‘C’ do #target 僅繼承 父級的搜索路勁 inherit! :search_paths end end
三、 Target configuration 這些設置來控制cocoapods生成工程。說明使用在什么平臺上
-
platform :用于指明應建立的靜態(tài)庫的平臺。
cocoapods默認指定的平臺配置: · iOS->4.3 · OS X->10.6 · tvOS->9.0 · watchOS->2.0 如果部署target需要iOS<4.3,armv6架構講被添加到ARCHS 使用: #指定具體平臺和版本 platform :ios, ‘4.0' platform :ios -
Inhibit_all_warnings! :忽略cocoapods依賴庫的警告??梢匀?定義,也可以定義在子target中,也可以指定某個庫
pod 'SSZipArchive', :inhibit_warnings => true pod 'SSZipArchive', :inhibit_warnings => false -
use_frameworks! :通過指定 use_frameworks! 要求生成的是framework而不是靜態(tài)庫,swift沒有靜態(tài)庫。
*如果使用use_frameworks!命令會在Pods工程下的Frameworks目錄下生成依賴庫的framework* *如果不使用use_frameworks!命令會在Pods工程下的Products目錄下生成.a的靜態(tài)庫* -
user_modular_headers!: 使用此命令表示所有cocoapods靜態(tài)庫都是用模塊化Headers
//指定某個pod使用模塊化 pod 'SSZipArchive', :modular_headers => true pod 'SSZipArchive', :modular_headers => false
四、workspace
-
workspace : 默認情況下,我們不需要指定,直接使用與Podfile所在目錄的工程名一樣就可以了。如果要指定另外的名稱,而不是使用工程的名稱,可以這樣指定:
workspace 'MyWorkspace'
五、source
-
source: 是指定pod的來源。如果不指定source,默認是使用CocoaPods官方的source。(建議使用默認設置)
# 使用其他來源地址 source 'https://github.com/artsy/Specs.git’ # 使用官方默認地址(默認) source 'https://github.com/CocoaPods/Specs.git'主要:如果使用自己的私有庫,需要使用source指定到私有庫地址
六、Hooks Podfile提供了hook機制,它將在安裝過程中調用。hook是全局性的,不存儲于每個target中。
-
Plugin :指定應在安裝期間使用的插件。使用此方法指定應在安裝期間使用的插件,以及當它被調用時,應傳遞給插件的選項。例如:
# 指定在安裝期間使用cocoapods-keys和slather這兩個插件 plugin 'cocoapods-keys', :keyring => 'Eidolon' plugin 'slather’ -
pre_install: 當我們下載完成,但是還沒有安裝之時,可以使用hook機制通過pre_install指定要做更改,更改完之后進入安裝階段。格式如下:
pre_install do |installer| # 做一些安裝之前的更改 end -
post_install:當我們安裝完成,但是生成的工程還沒有寫入磁盤之時,我們可以指定要執(zhí)行的操作。 比如,我們可以在寫入磁盤之前,修改一些工程的配置:
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 -
def :def命令來聲明一個pod集:
def 'CustomPods’ pod 'IQKeyboardManagerSwift’ end //然后,我們就可以在需要引入的target處引入 target 'MyTarget' do CustomPods end作用:如果有多個target,而不同target之間并不全包含,那么可以通過這種方式來分開引入。