CocoaPods使用中級篇

目錄

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 <UIKit/UIKit.h>'
      spec.prefix_header_contents = '#import <UIKit/UIKit.h>', '#import <Foundation/Foundation.h>'
    
    • 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??梢允褂胹pec中所有屬性。比如不同的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的介紹 什么是CocoaPods?CocoaPods是一個負責管理iOS項目中第三方開源庫...
    輝712閱讀 3,993評論 0 7
  • 項目組件化、平臺化是技術公司的共同目標,越來越多的技術公司推崇使用pod管理第三方庫以及私有組件,一方面使項目架構...
    swu_luo閱讀 22,226評論 0 39
  • 前言 前幾天發布我的開源庫<最簡單方便的iOS輪播開源庫:JYCarousel>到CocoaPods的時候。對Co...
    Dely閱讀 17,441評論 12 82
  • CocoaPods 是什么? CocoaPods 是一個負責管理 iOS 項目中第三方開源庫的工具。CocoaPo...
    朝洋閱讀 25,742評論 3 50
  • 背景 上一篇博客最新的CocoaPods的使用教程(一)里主要講解了CocoPods得簡單的日常使用。但是我們經常...
    Dely閱讀 5,899評論 11 34