基礎-Podfile講解

2.podfile文件講解

podfile是一個規(guī)范文件,描述一個或多個項目目標依賴項,CocoaPods管理iOS組件庫

各種參數(shù)配置含義

一、Install! :適用于整個podfile文件

  1. 指明了cocoapods安裝podfile使用的安裝方法和配置項
     install! 'cocoapods', //第一個參數(shù)是安裝方法,剩下的參數(shù)是選擇項,現(xiàn)在安裝方法只支持’cocoapods’
     :deterministic_uuids => false, 
     :integrate_targets => false 
    
  2. 還有以下配置參數(shù)
     :clean
     :deduplicate_targets
     :deterministic_uuids
     :integrate_targets
     :lock_pod_sources
     :warn_for_multiple_pod_sources
     :share_schemes_for_development_pods
    

    正常情況下 不需要配置這個參數(shù)

  3. build configurations (編譯配置) 默認情況下,依賴項會被安裝在所有target的build configrations中。
     //為了調試或者其他原因,他們可以在給定的configurations中啟用
     pod 'PonyDebugger', :configurations => ['Debug', 'Beta']
     //或者,你可以至指定一個build configration
     pod 'PonyDebugger', :configuration => ‘Debug'
    

    注意:默認情況下依賴會包含在所有配置中,如果你想指定需要手動配置。

  1. source: 默認被指定的依賴項會在全局級別的指定源中匹配搜索??梢詾樘匾蕾囮P系指定源
     //指定特定源中搜索,并忽略任何全局源*
     pod 'PonyDebugger', :source => 'https://github.com/CocoaPods/Specs.git'
    
  2. Subspecs: ##### 當使用依賴庫名字引入依賴庫時,也會默認安裝依賴庫中的所有子模塊。

     //指定引用指定子模塊
     pod 'QueryKit/Attribute’
     //指定一個子模塊集合
     pod 'QueryKit', :subspecs => ['Attribute', 'QuerySet']
    

二、依賴(Dependencies)

  1. 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 版本 ,安裝這個范圍內最新的版本

  1. 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'
    
  2. 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         
    
  3. 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。

  1. 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)]

  2. abstract! 指定當前target是抽象target
     target ‘A’ do
         abstract!
     end
    
  3. 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生成工程。說明使用在什么平臺上

  1. 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
    
  2. Inhibit_all_warnings! :忽略cocoapods依賴庫的警告??梢匀?定義,也可以定義在子target中,也可以指定某個庫
     pod 'SSZipArchive', :inhibit_warnings => true
     pod 'SSZipArchive', :inhibit_warnings => false
    
  3. use_frameworks! :通過指定 use_frameworks! 要求生成的是framework而不是靜態(tài)庫,swift沒有靜態(tài)庫。
     *如果使用use_frameworks!命令會在Pods工程下的Frameworks目錄下生成依賴庫的framework*
     *如果不使用use_frameworks!命令會在Pods工程下的Products目錄下生成.a的靜態(tài)庫*
    
  4. user_modular_headers!: 使用此命令表示所有cocoapods靜態(tài)庫都是用模塊化Headers
     //指定某個pod使用模塊化
     pod 'SSZipArchive', :modular_headers => true                
     pod 'SSZipArchive', :modular_headers => false
    

四、workspace

  1. workspace : 默認情況下,我們不需要指定,直接使用與Podfile所在目錄的工程名一樣就可以了。如果要指定另外的名稱,而不是使用工程的名稱,可以這樣指定:
     workspace 'MyWorkspace'
    

五、source

  1. 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中。

  1. Plugin :指定應在安裝期間使用的插件。使用此方法指定應在安裝期間使用的插件,以及當它被調用時,應傳遞給插件的選項。例如:
     # 指定在安裝期間使用cocoapods-keys和slather這兩個插件 
     plugin 'cocoapods-keys', :keyring => 'Eidolon' plugin 'slather’ 
    
  2. pre_install: 當我們下載完成,但是還沒有安裝之時,可以使用hook機制通過pre_install指定要做更改,更改完之后進入安裝階段。格式如下:
      pre_install do |installer| 
          # 做一些安裝之前的更改 
     end
    
  3. 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 
    
  4. def :def命令來聲明一個pod集:
     def 'CustomPods’ 
         pod 'IQKeyboardManagerSwift’ 
     end 
     //然后,我們就可以在需要引入的target處引入
     target 'MyTarget' do CustomPods 
     end 
    

    作用:如果有多個target,而不同target之間并不全包含,那么可以通過這種方式來分開引入。

?著作權歸作者所有,轉載或內容合作請聯(lián)系作者
【社區(qū)內容提示】社區(qū)部分內容疑似由AI輔助生成,瀏覽時請結合常識與多方信息審慎甄別。
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發(fā)布,文章內容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關閱讀更多精彩內容

  • 前言 iOS開發(fā)會經(jīng)常用到cocoapods管理第三方,簡單、方便、高效。如何集成cocoapods在cocoap...
    Moker_C閱讀 920評論 0 1
  • 經(jīng)常使用CocoaPods來管理iOS項目中的第三方庫,但是我們要使用CocoaPods來管理第三方庫,前提是要寫...
    qitianjin閱讀 1,434評論 2 0
  • 轉載自:http://blog.csdn.net/qitianjin/article/details/517738...
    YYT1992閱讀 2,800評論 0 0
  • 項目組件化、平臺化是技術公司的共同目標,越來越多的技術公司推崇使用pod管理第三方庫以及私有組件,一方面使項目架構...
    swu_luo閱讀 22,873評論 0 39
  • 瓢蟲:蟑螂大俠武功卓絕。我曾聽說,他孤身斬殺了幾十只大黃蜂。 螽斯:對對,我也聽說了。 流星蛛:一只蟑螂斬殺幾十只...
    風滿樓1992閱讀 298評論 0 2

友情鏈接更多精彩內容