CocoaPods學習02-PodSpec

CocoaPods學習01-Podfile
CocoaPods學習03-pod install vs pod update
CocoaPods學習04-制作自己的pod庫

.podspec文件,相當于pod庫的一個映射描述文件

根屬性

  • name庫名 spec.name = 'AFNetworking'

  • version版本號 spec.version = '3.0.0'

  • authors作者 單人或者多人

    spec.author = 'ZJ' 
    spec.authors = 'AZJ','BZJ'  
    spec.authors = { 'AZJ' => 'azj@google.com',
    'BZJ' = > 'bzj@sina.cn'}
    
  • license 版權許可 spec.license = 'MIT'

  • homepage 主頁 spec.homepage = 'http://www.zj.github.io'

  • source 庫源地址
    git 指定tag
    spec.source = { :git => 'https://github.com/AFNetworking/AFNetworking.git', :tag => spec.version.to_s }
    svn 指定tag
    spec.source = { :svn => 'http://svn.code.sf.net/p/polyclipping/code', :tag => '4.8.8' }
    http地址 支持的格式有zip, tgz, bz2, txz 和 tar
    指定hash碼 支持sha1和sha256
    spec.source = { :http => 'http://dev.wechatapp.com/download/sdk/WeChat_SDK_iOS_en.zip' :sha1 => '7e21857fe11a511f472cfd7cfa2d979bd7ab7d96'}

  • summary 摘要
    spec.summary = 'How to make love!'

以上都是必須設置的 ,下面的為可選

  • cocoapods_version支持的CocoaPods版本號 spec.cocoapods_version = '>= 0.36'
  • description描述
spec.description = <<-DESC
                     Computes the meaning of life.
                     Features:
                     1. Is self aware
                     ...
                     42. Likes candies.
                   DESC
  • screenshots 快照
spec.screenshot = [ 'http://dl.dropbox.com/u/378729/MBProgressHUD/1.png',
                     'http://dl.dropbox.com/u/378729/MBProgressHUD/2.png' ]`
  • documentation_url 文檔地址
    spec.documentation_url = 'http://www.example.com/docs.html'

  • requires_arc 是否需要arc環(huán)境
    spec.requires_arc = true

  • deprecated 棄用
    spec.deprecated = true

平臺

設置支持的平臺及版本號

spec.platform = :osx, '10.8'
spec.platform = :ios
spec.platform = :osx

spec.ios.deployment_target = '6.0'
spec.osx.deployment_target = '10.8'

編譯設置

  • dependency 依賴的其他庫
    spec.dependency 'AFNetworking', '~> 1.0'
    spec.ios.dependency 'MBProgressHUD', '~> 0.5'

  • frameworks 依賴的framework
    spec.ios.framework = 'CFNetwork'
    spec.frameworks = 'QuartzCore', 'CoreData'

  • libraries 依賴的libraries
    spe.ios.library = 'xml2'
    spe.libraries = 'xml2', 'z'

  • compiler_flags 編譯設置(如警告忽略,宏設置)
    spec.compiler_flags = '-Wno-format', '-DOS_OBJECT_USE_OBJC=0'

  • prefix_header_contents pch預編譯頭文件內(nèi)容
    spec.prefix_header_contents = '#import <UIKit/UIKit.h>'

  • prefix_header_file pch預編譯頭文件路徑
    spec.prefix_header_file = 'iphone/include/prefix.pch'

文件樣式

Podspecs應該放在repository的根目錄
*匹配所有,?匹配一個任意字符,[^a-d]匹配集合內(nèi)字符 {p,q}匹配p或q \跳過下一個

"JSONKit.?" 所有的jsonkit為名的文件
"*" 所有文件
"*.{h,m}" 所有的.h.m文件
  • source_files 源文件
    spec.source_files = 'Classes/**/*.{h,m}'
    spec.source_files = 'Classes/**/*.{h,m}', 'More_Classes/**/*.{h,m}'

  • public_header_files 公開頭文件 用戶可以引入查看的頭文件 ,不設置則搜有的頭文件都可以引入
    spec.public_header_files = 'Headers/Public/*.h'

  • private_header_files 私有頭文件 用戶不可以引入的頭文件
    spec.private_header_files = 'Headers/Private/*.h'

  • resource_bundles 資源包 推薦使用

spec.resource_bundles = {
   'MapBox' => ['MapView/Map/Resources/*.png'],
   'OtherResources' => ['MapView/Map/OtherResources/*.png']
 }
  • resources 資源包
    spec.resource = 'MJRefresh/MJRefresh.bundle'

  • subspec 子模塊依賴

實用示例

MJRefresh的spec

Pod::Spec.new do |s|
    s.name         = 'MJRefresh'
    s.version      = '3.1.15.1'
    s.summary      = 'An easy way to use pull-to-refresh'
    s.homepage     = 'https://github.com/CoderMJLee/MJRefresh'
    s.license      = 'MIT'
    s.authors      = {'MJ Lee' => 'richermj123go@vip.qq.com'}
    s.platform     = :ios, '6.0'
    s.source       = {:git => 'https://github.com/CoderMJLee/MJRefresh.git', :tag => s.version}
    s.source_files = 'MJRefresh/**/*.{h,m}'
    s.resource     = 'MJRefresh/MJRefresh.bundle'
    s.requires_arc = true
end

SDWebImage的spec

Pod::Spec.new do |s|
  s.name = 'SDWebImage'
  s.version = '4.2.2'

  s.osx.deployment_target = '10.8'
  s.ios.deployment_target = '7.0'
  s.tvos.deployment_target = '9.0'
  s.watchos.deployment_target = '2.0'

  s.license = 'MIT'
  s.summary = 'Asynchronous image downloader with cache support with an UIImageView category.'
  s.homepage = 'https://github.com/rs/SDWebImage'
  s.author = { 'Olivier Poitrey' => 'rs@dailymotion.com' }
  s.source = { :git => 'https://github.com/rs/SDWebImage.git', :tag => s.version.to_s }

  s.description = 'This library provides a category for UIImageView with support for remote '      \
                  'images coming from the web. It provides an UIImageView category adding web '    \
                  'image and cache management to the Cocoa Touch framework, an asynchronous '      \
                  'image downloader, an asynchronous memory + disk image caching with automatic '  \
                  'cache expiration handling, a guarantee that the same URL won\'t be downloaded ' \
                  'several times, a guarantee that bogus URLs won\'t be retried again and again, ' \
                  'and performances!'

  s.requires_arc = true
  s.framework = 'ImageIO'
  
  s.default_subspec = 'Core'

  s.subspec 'Core' do |core|
    core.source_files = 'SDWebImage/{NS,SD,UI}*.{h,m}'
    core.exclude_files = 'SDWebImage/UIImage+WebP.{h,m}', 'SDWebImage/SDWebImageWebPCoder.{h,m}'
    core.tvos.exclude_files = 'SDWebImage/MKAnnotationView+WebCache.*'
  end

  s.subspec 'MapKit' do |mk|
    mk.osx.deployment_target = '10.8'
    mk.ios.deployment_target = '7.0'
    mk.tvos.deployment_target = '9.0'
    mk.source_files = 'SDWebImage/MKAnnotationView+WebCache.*'
    mk.framework = 'MapKit'
    mk.dependency 'SDWebImage/Core'
  end

  s.subspec 'GIF' do |gif|
    gif.ios.deployment_target = '7.0'
    gif.source_files = 'SDWebImage/FLAnimatedImage/*.{h,m}'
    gif.dependency 'SDWebImage/Core'
    gif.dependency 'FLAnimatedImage', '~> 1.0'
    gif.xcconfig = {
      'USER_HEADER_SEARCH_PATHS' => '$(inherited) $(SRCROOT)/FLAnimatedImage/FLAnimatedImage'
    }
  end

  s.subspec 'WebP' do |webp|
    webp.source_files = 'SDWebImage/UIImage+WebP.{h,m}', 'SDWebImage/SDWebImageWebPCoder.{h,m}'
    webp.xcconfig = { 
      'GCC_PREPROCESSOR_DEFINITIONS' => '$(inherited) SD_WEBP=1',
      'USER_HEADER_SEARCH_PATHS' => '$(inherited) $(SRCROOT)/libwebp/src'
    }
    webp.watchos.xcconfig = {
      'GCC_PREPROCESSOR_DEFINITIONS' => '$(inherited) SD_WEBP=1 WEBP_USE_INTRINSICS=1',
      'USER_HEADER_SEARCH_PATHS' => '$(inherited) $(SRCROOT)/libwebp/src'
    }
    webp.dependency 'SDWebImage/Core'
    webp.dependency 'libwebp'
  end
end

參考地址
cocoapods guides

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

相關閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容