iOS架构-制作属于自己的cocoapods以及podspec文件讲解(20)

我们在 iOS架构-cocoaPods之自制私有库及管理(17) 中已经介绍了私有cocoapods库的制作。其中已经涉及podspec文件。这里单独拉出来介绍podspec文件。使大家对podspec文件的作用和用法有个清晰、明确的认识。

制作属于自己的cocoapods

iOS架构-cocoaPods之自制私有库及管理(17)
中已经实现过不再介绍

Podspec语法官方地址:

http://guides.cocoapods.org/syntax/podspec.html

.podspec文件讲解

podspec是一个描述pod库版本文件。

Specification

它包含源文件在哪里查找,要使用的文件,要应用的构建设置,以及常见元素名称、版本和说明
一个标准的podspec文件可以通过 $ pod spec create 命令生成

  1. 简例:
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.source_files = 'Reachability.{h,m}'
     spec.framework    = 'SystemConfiguration'
 end
  1. 详细例子
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', :t            ag => 'v3.1.0’ }    
     spec.module_name   = 'Rich'
     spec.swift_version = '4.0'
     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
文件中的字段注释
Pod::Spec.new do |spec|
    //库名称
    spec.name          = ‘Reachability’

    //库版本号,这也是我们podfile文件指定的版本号。 每次发布版本都需要打tag标签(名称就是版本号)
    spec.version       = ‘3.1.0'

    //许可证,除非源代码包含了LICENSE.*或者LICENCE.*文件,否则必须指定许可证文件。文件扩展名可以没有,或者是.txt,.md,.markdown
    spec.license       = { :type => 'BSD’ }
    or spec.license = ‘MIT'
    or spec.license = { :type => 'MIT', :file => 'MIT-LICENSE.txt’ }
    or spec.license = { :type => 'MIT', :text => <<-LICENSE
                        Copyright 2012
                         Permission is granted to...
                         LICENSE
                      }

    //pod主页
    spec.homepage      = 'https://github.com/tonymillion/Reachability’      

    //pod库维护者的名车和邮箱
    spec.authors       = { 'Tony Million' => 'tonymillion@gmail.com’ } 
    or spec.author = 'Darth Vader'
    or spec.authors = 'Darth Vader', 'Wookiee'
    or spec.authors = { 'Darth Vader' => 'darthvader@darkside.com',
                    'Wookiee'     => 'wookiee@aggrrttaaggrrt.com' }     

    //指定多媒体地址,如果是推特发布版本会有通知
    spec.social_media_url = 'https: //twitter.com/cocoapods 
    or spec.social_media_url = 'https: //groups.google.com/forum/#!forum/cocoapods'
    
    //pod简介
    spec.summary = 'ARC and GCD Compatible Reachability Class for iOS and OS X.'
        
    //详细描述
    spec.description = <<-DESC                     Computes the meaning of life.
                 Features:
                 1. Is self aware
                 ...
                 42. Likes candies.
               DESC
    
    //获取库的地址
    a. Git:git地址,tag:值以v开头,支持子模块
        spec.source        = { :git => 'https://github.com/tonymillion/Reachability.git', :tag => 'v3.1.0’ }
        spec.source = { :git => 'https://github.com/typhoon-framework/Typhoon.git',
                :tag => "v#{spec.version}", :submodules => true }
    b. Svn:svn地址
        spec.source = { :svn => 'http://svn.code.sf.net/p/polyclipping/code', :tag => ‘4.8.8‘ }
    c. Hg:Mercurial
        spec.source = { :hg => 'https://bitbucket.org/dcutting/hyperbek', :revision => "#{s.version}" }
    
    // Pod 屏幕截图,支持单个或者数组,主要适用于UI类的pod库。cocoapods推荐使用gif
    spec.screenshot  = 'http://dl.dropbox.com/u/378729/MBProgressHUD/1.png'
    or spec.screenshots = [ 'http://dl.dropbox.com/u/378729/MBProgressHUD/1.png',
                            'http://dl.dropbox.com/u/378729/MBProgressHUD/2.png' ]
    
    // 说明文档地址
    spec.documentation_url  =  'http://www.example.com/docs.html’
    
    // pod下载完成之后,执行的命令。可以创建,删除,修改任何下载的文件。该命令在pod清理之前和pod创建之前执行。
    spec.prepare_command = 'ruby build_files.rb'
    or spec.prepare_command = <<-CMD                        sed -i 's/MyNameSpacedHeader/Header/g' ./**/*.h
                    sed -i 's/MyNameOtherSpacedHeader/OtherHeader/g' ./**/*.h
             CMD
            
    //module name
    spec.module_name   = ‘Rich'
    //支持的swift版本
    spec.swift_version = ‘4.0'
    // 支持的Cocoapods版本
    spec.cocoapods_version = ‘>=0.36’
    
    // 是否使用静态库。如果podfile指明了use_frameworks!命令,但是pod仓库需要使用静态库则需要设置
    spec.static_framework = true
    
    // 库是否废弃
    spec.deprecated = true

    //  废弃的pod名称
    spec.deprecated_in_favor_of = 'NewMoreAwesomePod'

    // pod支持的平台,如果没有设置意味着支持所有平台,使用deployment_target支持选择多个平台
    spec.platform = :osx,  ’10.8'
    or spec.platform = :ios
    or spec.platform = :osx         
            
    // 可以指定多个不同平台
    spec.ios.deployment_target =  ‘6.0'
    or .osx.deployment_target = '10.8'
配置 pod库 工程环境变量
  1. dependency: 私有库依赖的三方库
spec.dependency 'AFNetworking', '~> 1.0'
  1. requires_arc: 指定私有库 文件是否 是ARC.默认是true,表示所有的 source_files是arc文件
spec.requires_arc = true
 //指定除了Arc文件下的是arc,其余的全还mrc,会添加-fno-objc-arc 编辑标记
 spec.requires_arc = false       spec.requires_arc = 'Classes/Arc'
 spec.requires_arc = ['Classes/*ARC.m', 'Classes/ARC.mm']

注意:spec.requires_arc 指定的路径表示是arc文件,不被指定才会被标记 -fno-objc-arc

  1. frameworks: pod库使用的系统库
spec.ios.framework = 'CFNetwork'
 spec.frameworks = 'QuartzCore', 'CoreData'
  1. weak_frameworks: 如果在高版本的OS中调用新增的功能,还要保持低版本OS能够运行,就要使用weak_framwoks如果引用的某些类或者接口在低版本中并不支持,可以再运行时判断。
spec.weak_framework = 'Twitter'     
  1. libraries使用的静态库 比如libz,sqlite3.0等,多个用逗号分开
spec.ios.library = ‘xml2'
 spec.libraries = 'xml2', 'z'
  1. compiler_flags: 传递个编译器的标记列表
spec.compiler_flags = '-DOS_OBJECT_USE_OBJC = 0' , '-Wno-format'
  1. pod_target_xcconfig: flag添加到私有库的target Xcconfig文件中,只会配置当前私有库
//user_target_xcconfig  会影响所有target的 Xcconfig,他会污染用户配置,所以不推荐使用,可能会导致冲突
 spec.pod_target_xcconfig  =  {  'OTHER_LDFLAGS'  =>  '-lObjC'  }
 spec.user_target_xcconfig  =  {  'MY_SUBSPEC'  =>  'YES'  }

注意:尽量使用pod_target_xcconfig,只会影响你编译的pod

  1. prefix_header_file: 默认是true cocoapos会生成默认前缀.pch文件
//自定义前缀文件
     spec.prefix_header_file = false
     spec.prefix_header_file = 'iphone/include/prefix.pch'
  1. prefix_header_contents 向pod项目的前缀文件中添加内容
spec.prefix_header_contents  =  '#import <UIKit / UIKit.h>'
 spec.prefix_header_contents  =  '#import <UIKit / UIKit.h>' , '#import <Foundation / Foundation.h>'
  1. module_name
    模块对外的名称,如果设置了,主工程链接的时候用的是指定的名称。
spec.module_name = 'Three20'
  1. header_dir
spec.header_dir = 'Three20Core'
  1. header_mappings_dir
spec.header_mappings_dir = 'src/include'
File patterns 文件格式

文件操作 :podspec文件必须在根仓库文件中。文件路径也是相对于根仓库位置的

  • source_files pod库的源文件
#包含Classes目录下的所有.h .m文件
spec.source_files = 'Classes/**/*.{h,m}'
#包含Classes和More_Classes目录下的所有.h .m文件
spec.source_files = 'Classes/**/*.{h,m}', 'More_Classes/**/*.{h,m}'
通配符*:

匹配所有文件
c
匹配名字以c开头的文件。
*c 匹配名字以c结束的文件。
c 匹配名字含有c的,包含c在开头和结尾的情况。

通配符**:

目录递归地匹配。也就是包含子目录

通配符?:

匹配任何一个字符
与正则中 /.{1}/ 一致

通配符[set]:

匹配多个字符。匹配在字符集中的任何一个字符。
跟正则中的字符集一样,也可以取反 [^a-z]

通配符{p,q}:

匹配文件名包含p或q的,可以写两个或多个字

通配符\:

跳过下一个元字符

#以JSONKit为例
"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"]
  • public_header_files
    pod库暴露给用户工程的头文件。如果不指定那么source_files中的所有header都被认为是公共的。
spec.public_header_files = 'Headers/Public/*.h'
  • private_header_files
    设置私有头文件
spec.private_header_files = 'Headers/Private/*.h'
  • vendored_frameworks
    pod库中framework的路径
spec.ios.vendored_frameworks = 'Frameworks/MyFramework.framework'
spec.vendored_frameworks = 'MyFramework.framework', 'TheirFramework.framework'
  • vendored_libraries
    pod库中的静态库
spec.ios.vendored_library = 'Libraries/libProj4.a'
spec.vendored_libraries = 'libProj4.a', 'libJavaScriptCore.a'
  • resource_bundles
    pod中的资源会以bundle的形式添加到项目中。键表示bundles的名称,值表示文件格式。
    推荐使用,bundle的名称应该包括Pod的名称,以尽量减少名称冲突的几率。
    为了提供不同的资源,每个平台都必须使用带有名称空间的包。
spec.ios.resource_bundle = { 'MapBox' => 'MapView/Map/Resources/*.png' }
#多个路径
spec.resource_bundles = {
    'MapBox' => ['MapView/Map/Resources/*.png'],
    'OtherResources' => ['MapView/Map/OtherResources/*.png']
  }
  • resources
    使用此属性指定的资源直接复制到客户端目标,因此不会被Xcode优化,推荐使用resource_bundles
spec.resource = 'Resources/HockeySDK.bundle'
#多个,注意是resources复数形式,也可以不加[ ]
spec.resources = ['Images/*.png', 'Sounds/*']
  • exclude_files
    排除在外的文件,与source_files相对
spec.ios.exclude_files = 'Classes/osx'
spec.exclude_files = 'Classes/**/unused.{h,m}'
  • preserve_paths
    下载后不会被删除的文件。默认删除不匹配任何file pattern的文件。
spec.preserve_path = 'IMPORTANT.txt'

spec.preserve_paths = 'Frameworks/*.framework'
  • module_map
    pod被组装成framework的时候module map文件可能用的上,默认的cocoapod会基于public headers 创建一个module map。
spec.module_map = 'source/module.modulemap'
Subspecs

可以理解为pod库中的子模块。
一个库可以指定对另一个库的依赖,另一个库的子规范,或者是它自身的子规范。

  • subspec
    用来表示pod库模块的规范
    一方面,spec自动将subspecs作为依赖项(除非指定了默认的子规范)。
    另一方面,“子规范”继承了父属性的值,因此可以在父类中指定属性的共同值。
#安装ShareKit,会包括ShareKit / Evernote,ShareKit / Facebook等,因为它们被定义为subspecs。
pod 'ShareKit', '2.0'
#只安装ShareKit中的某个子库,这种情况下subspec需要源文件,依赖和其他在根spec中定义的属性,不过cocoapods能帮我们处理这些问题。
pod 'ShareKit/Twitter',  '2.0'
pod 'ShareKit/Pinboard', '2.0'
#有不同源文件的subspec
subspec 'Twitter' do |sp|
  sp.source_files = 'Classes/Twitter'
end

subspec 'Pinboard' do |sp|
  sp.source_files = 'Classes/Pinboard'
end
#子库的spec依赖其他subspec·
Pod::Spec.new do |s|
  s.name = 'RestKit'

  s.subspec 'Core' do |cs|
    cs.dependency 'RestKit/ObjectMapping'
    cs.dependency 'RestKit/Network'
    cs.dependency 'RestKit/CoreData'
  end

  s.subspec 'ObjectMapping' do |os|
  end
end
#嵌套的Subspec
Pod::Spec.new do |s|
  s.name = 'Root'

  s.subspec 'Level_1' do |sp|
    sp.subspec 'Level_2' do |ssp|
    end
  end
end
  • default_subspecs
    一组用来作为依赖项的subspec名称。如果没有指定则要求其所有的subspec作为依赖项。
    默认情况下pod库应该提供完整的库。用户可以根据需求微调他们的依赖项,排除不需要的子模块(subspec)。
    这个属性很少用。
spec.default_subspec = 'Core'

spec.default_subspecs = 'Core', 'UI'
Multi-Platform support

所有标识支持多平台的,都可以针对平台设置参数

spec.ios.resources = 'Resources_ios/**/*.png'
spec.osx.source_files = 'Classes/osx/**/*.{h,m}'
spec.tvos.source_files = 'Classes/tvos/**/*.{h,m}'
spec.watchos.source_files = 'Classes/watchos/**/*.{h,m}'
参考文章

Podspec语法参考 v1.2.0.beta.1
CocoaPods 系列之三 Podspec 语法说明

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值