基础-Podfile讲解

作者: coderPoo | 来源:发表于2018-09-04 17:00 被阅读133次

    2.podfile文件讲解

    podfile是一个规范文件,描述一个或多个项目目标依赖项,CocoaPods管理iOS组件库

    各种参数配置含义

    一、Install! :适用于整个podfile文件

    1. 指明了cocoapods安装podfile使用的安装方法和配置项
       install! 'cocoapods', //第一个参数是安装方法,剩下的参数是选择项,现在安装方法只支持’cocoapods’
       :deterministic_uuids => false, 
       :integrate_targets => false 
      
    2. 还有以下配置参数
       :clean
       :deduplicate_targets
       :deterministic_uuids
       :integrate_targets
       :lock_pod_sources
       :warn_for_multiple_pod_sources
       :share_schemes_for_development_pods
      

      正常情况下 不需要配置这个参数

    3. build configurations (编译配置) 默认情况下,依赖项会被安装在所有target的build configrations中。
       //为了调试或者其他原因,他们可以在给定的configurations中启用
       pod 'PonyDebugger', :configurations => ['Debug', 'Beta']
       //或者,你可以至指定一个build configration
       pod 'PonyDebugger', :configuration => ‘Debug'
      

      注意:默认情况下依赖会包含在所有配置中,如果你想指定需要手动配置。

    1. source: 默认被指定的依赖项会在全局级别的指定源中匹配搜索。可以为特依赖关系指定源
       //指定特定源中搜索,并忽略任何全局源*
       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在指定节点或者分支
       //主分支:
       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'
      
       //指定的节点: :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
      

      意义:可以使用抽象类 来统一 需要继承的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 :用于指明应建立的静态库的平台。
       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而不是静态库,swift没有静态库。
       *如果使用use_frameworks!命令会在Pods工程下的Frameworks目录下生成依赖库的framework*
       *如果不使用use_frameworks!命令会在Pods工程下的Products目录下生成.a的静态库*
      
    4. user_modular_headers!: 使用此命令表示所有cocoapods静态库都是用模块化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:当我们安装完成,但是生成的工程还没有写入磁盘之时,我们可以指定要执行的操作。 比如,我们可以在写入磁盘之前,修改一些工程的配置:
       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之间并不全包含,那么可以通过这种方式来分开引入。

    相关文章

      网友评论

        本文标题:基础-Podfile讲解

        本文链接:https://www.haomeiwen.com/subject/unkgwftx.html