CocoaPods使用中级篇

目录

  • profile相关参数

    • 依赖相关的指令
    • Target配置
    • Source
    • Hooks
    • def name end
  • podspec相关参数

    • 一些参数
  • 清除pod缓存

profile相关参数

1. 依赖相关的指令

  • pod : 声明一个依赖库
    • 版本
    //使用最新的版本
    pod 'SSZipArchive' 
    
    //指定使用某个版本
    pod 'Objection', '0.9'
    
    //0.1.2与0.2版本之间的最新版本
    pod 'Objection', '~> 0.1.2'
    
    • Build配置
    
      //只有在debug模式下才使用PonyDebugger库
      pod 'PonyDebugger', :configuration => 'Debug'
    
    • Subspecs 配置
      Subspecs 配置, 只使用pod中某一个或者某一些子pod      库(需要pod库支持)
    //使用QueryKit下的Attribute库:
    pod 'QueryKit/Attribute'
    
    //使用QueryKit下的Attribute库和QuerySet库:
    pod 'QueryKit', :subspecs => ['Attribute', 'QuerySet']
    
    • 本地pod库(cocoapod会在指定的文件夹下找podspec,所以当前文件夹下一定要有podspec文件)
        pod 'AFNetworking', :path => '~/Documents/AFNetworking'
      
    • 设置pod库
      //使用master分支
      pod 'AFNetworking', :git => 'https://github.com/gowalla/AFNetworking.git'
    
      //使用指定分支
      pod 'AFNetworking', :git =>         'https://github.com/gowalla/AFNetworking.git', :branch => 'dev'
    
      //使用指定tag
      pod 'AFNetworking', :git => 'https://github.com/gowalla/AFNetworking.git', :tag => '0.7.0'
    
      //使用某一次commit
      pod 'AFNetworking', :git => 'https://github.com/gowalla/AFNetworking.git', :commit => '082f8319af
    
    • 设置podSpec的来源
pod 'JSONKit', :podspec => 'https://example.com/JSONKit.podspec'
  • Target : 与project中的target保持一致,默认target会继承外面的依赖库

简单应用:

target 'ZipApp' do
  pod 'SSZipArchive'
end

继承别的target的依赖库

target 'ZipApp' do
  pod 'SSZipArchive'

  target 'ZipAppTests' do
    inherit! :search_paths
    pod 'Nimble'
  end
end

继承多个target的依赖库(ShowsTests 同时有ShowsApp和)

target 'ShowsApp' do
  pod 'ShowsKit'

  # Has its own copy of ShowsKit + ShowTVAuth
  target 'ShowsTV' do
    pod 'ShowTVAuth'
  end

  target 'ShowsTests' do
    inherit! :search_paths
    pod 'Specta'
    pod 'Expecta'
  end
end
  • 抽象Target : 方便其他target继承(这个抽象的target是不存在的)
abstract_target 'Networking' do
  pod 'AlamoFire'

  target 'Networking App 1'
  target 'Networking App 2'
end
# Note: There are no targets called "Shows" in any of this workspace's Xcode projects
abstract_target 'Shows' do
  pod 'ShowsKit'

  # The target ShowsiOS has its own copy of ShowsKit (inherited) + ShowWebAuth (added here)
  target 'ShowsiOS' do
    pod 'ShowWebAuth'
  end

  # The target ShowsTV has its own copy of ShowsKit (inherited) + ShowTVAuth (added here)
  target 'ShowsTV' do
    pod 'ShowTVAuth'
  end

  # Our tests target has its own copy of
  # our testing frameworks, and has access
  # to ShowsKit as well because it is
  # a child of the abstract target 'Shows'

  target 'ShowsTests' do
    inherit! :search_paths
    pod 'Specta'
    pod 'Expecta'
  end
end
  • inherit!:给当前target设置集成模式

参数有

:complete : 全部继承父target
:none :全部不继承
:search_paths :只继承父target的searchPaths

例如:
target 'App' do
  target 'AppTests' do
    inherit! :search_paths
  end
end

2. Target配置

platform: 两个参数。一个是name, 一个是target
name属性
osx for OS X, :ios for iOS, :tvos for tvOS, or :watchos for watchOS.

target:版本

例如:

platform :ios, '4.0'
platform :ios
inhibit_all_warnings : 忽略所有库中的警告,也可以指定某一个具体库的警告。例如:
//全部忽略
platform :ios, '8.0'

inhibit_all_warnings!

source 'https://github.com/CocoaPods/Specs.git'

target 'TestPod' do

  pod 'SummerOCProjectFrame', '~> 0.0'

end

//某一个忽略
pod 'SummerOCProjectFrame', '~> 0.0', :inhibit_warnings => true

3. Source : 可以指定CocoaPods Master Repository,也可以自定义的Repository。例如:

source 'https://github.com/artsy/Specs.git'
source 'https://github.com/CocoaPods/Specs.git'

4. Hooks: 提供在初始化过程中接口

plugin:插件。描述在初始化时候使用的插件。例如:
plugin 'cocoapods-keys', :keyring => 'Eidolon'
plugin 'slather'
pre_install:在pod下载完初始化前可以通过它做逻辑处理。例如:
pre_install do |installer|
  # Do something fancy!
end
post_install:在形成project后写入本地前可以通过它做逻辑处理。比如设置target的build setting, 代码如下:
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

5. def name end。 如果project依赖的库很多,比如有自己开发的库,有第三方的库,可以对这些库进行分类。例如:

def myPods
pod xxx
pod xxx1
pod xxx2
end

def thirdPods
pod yyy
pod yyy1
pod yyy2
end

target 'xxx' do
  myPods
  thirdPods
end

参考:https://guides.cocoapods.org/syntax/podfile.html#podfile

podspec相关参数

  • 一些参数:

    • prefix_header_file : 给pod库设置pch文件
    • dependency : 设置依赖库
    • weak_framework :兼容高低版本api的问题,类似于主工程中frame里面的option属性
    • libraries, framework
    • compiler_flags,例如
    spec.compiler_flags = '-DOS_OBJECT_USE_OBJC=0', '-Wno-format'
    
    • prefix_header_contents :给pod库的pch文件增加文件,例如:
      spec.prefix_header_contents = '#import '
      spec.prefix_header_contents = '#import ', '#import '
    
    • source_files, public_header_files(不写默认是所有.h文件),private_header_files。source支持如下几种源:
    //git源 :git => :tag, :branch, :commit, :submodules
    spec.source = { :git => 'https://github.com/AFNetworking/AFNetworking.git',
                  :tag => spec.version.to_s }
    
    //:svn => :folder, :tag, :revision
    spec.source = { :svn => 'http://svn.code.sf.net/p/polyclipping/code', :tag => '4.8.8' }
    
    //:hg => :revision
    spec.source = { :hg => 'https://bitbucket.org/dcutting/hyperbek', :revision => "#{s.version}" }
    
    //:http => :flatten, :type, :sha256, :sha1
     spec.source = { :http => 'http://dev.wechatapp.com/download/sdk/WeChat_SDK_iOS_en.zip' }
    
    //:path
    
    • vendored_frameworks: pod库的frame
    • vendored_libraries: 第三方.a文件
spec.vendored_libraries  = 'xxx/Classes/ThirdParty/*.{a}'
  • resource_bundles,resources
spec.resource_bundles = {
    'MapBox' => ['MapView/Map/Resources/*.png'],
   'OtherResources' => ['MapView/Map/OtherResources/*.png']
  }

spec.resources = ['Images/*.png', 'Sounds/*']
  • preserve_path: install 或者 update之后不会更新的文件路径
spec.preserve_path = 'IMPORTANT.txt'
spec.preserve_paths = 'Frameworks/*.framework'  
  • exclude_files : 不包含的文件。例如:
spec.exclude_files = 'Classes/**/unused.{h,m}'
  • subspec : 子spec。可以使用spec中所有属性。比如不同的source_file, 不同的dependencies。例如:
 //不同的source_file
subspec 'Twitter' do |sp|
  sp.source_files = 'Classes/Twitter'
end

subspec 'Pinboard' do |sp|
  sp.source_files = 'Classes/Pinboard'
end

  Pod::Spec.new do |s|
  s.name = 'RestKit'

  //不同的dependencies
  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

参考:https://guides.cocoapods.org/syntax/podspec.html

清除pod缓存

第一步:
pod cache list //查看看缓存
pod cache clean --all //清除缓存 

如果还不行,就执行下面的代码:
rm ~/Library/Caches/CocoaPods/search_index.json;
pod sutup

你可能感兴趣的:(CocoaPods使用中级篇)