Cocoapods基礎(chǔ)使用

Cocoapods基礎(chǔ)使用

基礎(chǔ)使用

創(chuàng)建一個(gè)Cocoapods管理的Xcode項(xiàng)目

  1. 創(chuàng)建一個(gè)Xcode項(xiàng)目

  2. 打開終端,cd到工程目錄下,使用pod init命令,會(huì)初始化一個(gè)Podfile的文件

  3. 打開Podfile文件, 第一行指定我們使用的平臺(tái)以及版本,例如:platform :ios, '9.0',指定支持iOS9之后

  4. 為了使cocoapods關(guān)聯(lián)我們指定的項(xiàng)目,我們需要添加target '$TARGET_NAME' do end,在end前pod要添加的庫名即可

    target 'MyApp' do
        pod 'ObjectiveSugar'
    end
    
  5. 保存修改的Podfile文件,執(zhí)行pod install命令即可

當(dāng)需要像一個(gè)已經(jīng)存在的xcworkspace中集成cocoapod功能時(shí),只需要在目標(biāo)塊外部添加一行來指定workspace即可:workspace 'MyWorkspace'

cocoapod做了哪些事情

  1. cocoapod創(chuàng)建了一個(gè)workspace
  2. 將我們的工程添加到該workspace,
  3. 添加需要的靜態(tài)庫到該workspace
  4. pod庫之后都會(huì)打包成libPods.a,將該文件link到工程
  5. pod庫的編譯依賴參數(shù)等變?yōu)閹斓?xcconfig文件,根據(jù)該文件向項(xiàng)目中添加Xcode configuration設(shè)置在編譯時(shí)的依賴參數(shù)
  6. 改變項(xiàng)目 target configurations基于Cocoapods
  7. 添加工程執(zhí)行script為Pods-resources.sh的bash腳本,然后在每次編譯時(shí)將pod庫中的資源文件復(fù)制

pod install && pod update

pod install

pod install只在我們每次修改了podfile文件,增加、刪除、修改了依賴庫時(shí),才需要執(zhí)行該命令來檢索指定庫
當(dāng)執(zhí)行pod install后,會(huì)將我們使用的庫的版本號(hào)寫入podfile.lock文件中,此文件用來追蹤我們使用的pod庫的版本號(hào)
執(zhí)行pod install,會(huì)下載Podfile.lock文件中已存在的pod庫的指定版本,或者不在podfile.lock中的pod庫的合適版本

pod outdated

此命令會(huì)根據(jù)podfile.lock中的版本,列出所有有較新版本的pod庫,這就是我們執(zhí)行pod update或者 pod update PODNAME會(huì)更新的版本內(nèi)容(這些寫版本也是會(huì)受限于我們的podfile中設(shè)置的pod庫版本號(hào) pod 'MyPod', '~>x.y')

pod outdated

會(huì)檢索比podfile.lock中版本更新的庫,并更新(只要該版本符合podfile中版本的限制)
pod update PODNAME 更新一個(gè)
pod update 更新所有

因?yàn)閜odfile.lock是記錄我們pod庫版本的,因此我們需要向遠(yuǎn)程倉庫中commit、push該文件

Podfile

一個(gè)比較復(fù)雜的podfile文件如下

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

platform :ios, '9.0'
inhibit_all_warnings!

target 'MyApp' do
  pod 'GoogleAnalytics', '~> 3.1'

  # Has its own copy of OCMock
  # and has access to GoogleAnalytics via the app
  # that hosts the test target

  target 'MyAppTests' do
    inherit! :search_paths
    pod 'OCMock', '~> 2.0.1'
  end
end

post_install do |installer|
  installer.pods_project.targets.each do |target|
    puts target.name
  end
end

具體用法可以查看cocoapod官網(wǎng),下面為部分用法解析:

多個(gè)Target共享pod庫

使用abstract_target來使多個(gè)目標(biāo)共享pod
abstract_target是抽象對(duì)象,只是為了讓方便繼承,并不存在實(shí)際該target

# 并沒有名為'Shows'的目標(biāo)工程
abstract_target 'Shows' do
  pod 'ShowsKit'
  pod 'Fabric'

  # 'ShowsiOS’工程包含'ShowsKit'和'Fabric'pod庫的拷貝
  target 'ShowsiOS' do
    pod 'ShowWebAuth'
  end

  # 'ShowsTV’工程包含'ShowsKit'和'Fabric'pod庫的拷貝
  target 'ShowsTV' do
    pod 'ShowTVAuth'
  end
end

可以簡寫為 和我們上面復(fù)雜的的Podfile 例子一樣了

pod 'ShowsKit'
pod 'Fabric'

target 'ShowsiOS' do
  pod 'ShowWebAuth'
end

target 'ShowsTV' do
  pod 'ShowTVAuth'
end

指定版本

  • 指定特別的版本

    • pod 'SSZipArchive' 用最新的pod庫版本
    • pod 'Objection', '0.9' 指定使用的為特定的pod庫版本
    • pod 'Objection', '>0.1' 指定任何大于0.1的版本
    • pod 'Objection', '>=0.1' 指定任何大于或等于0.1的版本
    • pod 'Objection', '<0.1' 指定任何小于0.1的版本
    • pod 'Objection', '<=0.1' 指定任何小于或等于0.1的版本
    • pod 'Objection', '~>0.1.2' 指定版本在0.1.2到0.2之間 不包括0.2
    • pod 'Objection', '~>0.1' 指定版本在0.1.2到1.0之間 不包括1.0

指定pod的來源

指定pod來源為本地文件

常用于pod發(fā)布之前的本地調(diào)試

# 
pod 'Alamofire', :path => '~/Documents/Alamofire'
#Pod文件的podspec應(yīng)該位于指定的文件夾中

自定義pod的git分支或版本

有時(shí)候?qū)τ谖覀冏约簞?chuàng)建的pod庫,我們可能需要pod特定版本或者指定分支
此時(shí),在該庫的根目錄中應(yīng)該包含podspec文件

# pod git 上master分支的版本
pod 'Alamofire', :git => 'https://github.com/Alamofire/Alamofire.git'

# pod dev分支的版本庫
pod 'Alamofire', :git => 'https://github.com/Alamofire/Alamofire.git', :branch => 'dev'

# pod 指定tag的庫
pod 'Alamofire', :git => 'https://github.com/Alamofire/Alamofire.git', :tag => '3.1.1'

# pod 指定commit號(hào)版本
pod 'Alamofire', :git => 'https://github.com/Alamofire/Alamofire.git', :commit => '0f506b1c45'

指定特定的源

通常我們會(huì)在全局范圍搜索指定的庫,當(dāng)指定了該項(xiàng)后,我們對(duì)此pod庫將只搜索指定的源

pod 'PonyDebugger', :source => 'https://github.com/CocoaPods/Specs.git'

安裝子pod庫

我們通常在安裝pod時(shí) 會(huì)安裝定義在其中的所有的Subspecs
我們可以通過此方法安裝其中子庫

pod 'QueryKit/Attribute'

pod 'QueryKit', :subspecs => ['Attribute', 'QuerySet']

對(duì)于沒有podspec的庫,從spec集合庫外其他獲取

假如可以從其它來源,例如http請(qǐng)求,獲取到podspec

pod 'JSONKit', :podspec => 'https://example.com/JSONKit.podspec'

install!

生命安裝期間使用的安裝方法或選項(xiàng), 一般情況下,不需要特殊指定,使用默認(rèn)的就行

abstract!

指定當(dāng)前目標(biāo)是抽象的,因此不會(huì)鏈接到xcode目標(biāo)

inherit!

設(shè)置子target繼承父target的繼承模式, :none表示不繼承任何 :search_path之繼承父的searchPath

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

工程配置

inhibit_all_warnings!

禁止所有cocoapods的warning,可以被子target繼承或重定義

# 禁用特定庫的warning  
pod 'SSZipArchive', :inhibit_warnings => true

use_frameworks!

使用framework代替靜態(tài)庫 常用與swift項(xiàng)目

Source

Source是用來 從給定的source列表檢索指定pod庫的specs

我們可以指定specs位置

# 這是自定義源 源的順序與pod庫順序相關(guān) pod庫將使用第一個(gè)找到該庫的源的版本
source 'https://github.com/artsy/Specs.git'
# 這是官方源 其實(shí)隱含包括的 當(dāng)使用自定義source時(shí) 就需要顯示指定官方源
source 'https://github.com/CocoaPods/Specs.git'

Hooks

Hooks 包括 plugin pre_install post_install 等 會(huì)在安裝pod庫的進(jìn)程中調(diào)用進(jìn)行一些處理,詳情

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

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

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