Cocoapod詳解

引言:What? 搞iOS不知道CocoaPod

首先給出一個(gè)鏈接,通篇文章將完全是參照,官網(wǎng)的內(nèi)容進(jìn)行整理和總結(jié),也建議大家閱讀官網(wǎng)的文章,網(wǎng)上文件大多尋章摘句。
CocoaPod官網(wǎng)的使用指南
CocoaPod官網(wǎng)
以上兩個(gè)鏈接,可謂學(xué)習(xí)CocoaPod最好最全面的資料

一、What is CocoaPods

CocoaPods manages library dependencies for your Xcode projects.
The dependencies for your projects are specified in a single text file called a Podfile. CocoaPods will resolve dependencies between libraries, fetch the resulting source code, then link it together in an Xcode workspace to build your project.
Ultimately the goal is to improve discoverability of, and engagement in, third party open-source libraries by creating a more centralised ecosystem.

概括起來(lái)可以說(shuō)明為是 CocoaPod通過(guò)Podfile文件(一種通過(guò)簡(jiǎn)單文本描述來(lái)指定依賴庫(kù)的文件)可以解決類庫(kù)依賴,同時(shí)通過(guò)workSpace的形式添加到你的Xcode工程中。
其中繼目的就是建立一個(gè)更加中心化的生態(tài)系統(tǒng)來(lái)提升第三方庫(kù)管理。

二、Getting Started

To update CocoaPods you simply install the gem again

$ [sudo] gem install cocoapods

Or for a pre-release version

$ [sudo] gem install cocoapods --pre

任何安裝問(wèn)題可以點(diǎn)開該文章的鏈接,其中CocoaPod依賴于Ruby

三、Using CocoaPods

生成一個(gè)CocoaPods

$ pod init

具體的Podfile語(yǔ)法

platform :ios, '9.0'
target 'MyApp' do
  pod 'ObjectiveSugar'
end

之后的步驟

$ pod install

四、pod install vs. pod update

bundler, RubyGems or composer也有對(duì)對(duì)應(yīng)的語(yǔ)言,CocoaPod設(shè)計(jì)的和他們類似
pod install
適用于獲取pod,當(dāng)你編輯Podfile中存在add, update, remove a pod的需求的時(shí)候,可以使用該命令。

  • 根據(jù)Podfile.lock的內(nèi)容,搞定不在Podfile.lock里面的pods的依賴,不會(huì)去嘗試檢查更新的版本。
  • 對(duì)于不存在Podfile.lock里面的pod,將按照podfile里面的內(nèi)容進(jìn)行搜索對(duì)應(yīng)的版本

pod outdated
CocoaPods將羅列出在Podfile.lock擁有更新版本的pod,也就說(shuō)如果你執(zhí)行pod update PODNAME,如果新的版本依然符合Podfile里面的描述,那么將會(huì)被更新

pod update
兩種形式,pod update PODNAME將更新指定的pod,否則將更新所有。前提是符合Podfile里面的描述內(nèi)容

注意
基于podfile.lock的作用,所以當(dāng)倉(cāng)庫(kù)共享的時(shí)候,需要注意要提交podfile.lock。以方便統(tǒng)一的來(lái)鎖定依賴庫(kù)。

五、The Podfile

文件整體框架

簡(jiǎn)單的例子

target 'MyApp' do
  use_frameworks!
  pod 'Alamofire', '~> 3.0'
end

復(fù)雜的例子

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

如果想多個(gè)targets共用一些pod,考慮使用abstract_target

# There are no targets called "Shows" in any Xcode projects
abstract_target 'Shows' do
  pod 'ShowsKit'
  pod 'Fabric'

  # Has its own copy of ShowsKit + ShowWebAuth
  target 'ShowsiOS' do
    pod 'ShowWebAuth'
  end

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

隱藏抽象target,如下相當(dāng)于隱晦地使用抽象target

pod 'ShowsKit'
pod 'Fabric'

# Has its own copy of ShowsKit + ShowWebAuth
target 'ShowsiOS' do
  pod 'ShowWebAuth'
end

# Has its own copy of ShowsKit + ShowTVAuth
target 'ShowsTV' do
  pod 'ShowTVAuth'
end
具體的語(yǔ)法細(xì)則

指定版本規(guī)則
不指定版本

pod 'SSZipArchive'

凍結(jié)版本

pod 'Objection', '0.9'

邏輯運(yùn)算符指定

  • '> 0.1' Any version higher than 0.1
  • '>= 0.1' Version 0.1 and any higher version
  • '< 0.1' Any version lower than 0.1
  • '<= 0.1' Version 0.1 and any lower version

~>來(lái)指定

  • '~> 0.1.2' Version 0.1.2 and the versions up to 0.2, not including 0.2 and higher
  • '~> 0.1' Version 0.1 and the versions up to 1.0, not including 1.0 and higher
  • '~> 0' Version 0 and higher, this is basically the same as not having it.

指定本地文件機(jī)制

pod 'Alamofire', :path => '~/Documents/Alamofire'

指定對(duì)應(yīng)的git倉(cāng)庫(kù)
默認(rèn)使用master分支

pod 'Alamofire', :git => 'https://github.com/Alamofire/Alamofire.git'

使用其它分支

pod 'Alamofire', :git => 'https://github.com/Alamofire/Alamofire.git', :branch => 'dev'

指定tag

pod 'Alamofire', :git => 'https://github.com/Alamofire/Alamofire.git', :tag => '3.1.1'

指定對(duì)應(yīng)commit

pod 'Alamofire', :git => 'https://github.com/Alamofire/Alamofire.git', :commit => '0f506b1c45'

六、Making a CocoaPod

簡(jiǎn)單的建立CocoaPod可以參考以上連接,細(xì)化請(qǐng)看如下說(shuō)明。

Using Pod Lib Create

pod lib create SLLibrary

默認(rèn)的模板如下圖,當(dāng)然我們也可以采用自己的模板,通過(guò)增加如下參數(shù)為--template-url=URL


image.png

七、Getting setup with Trunk

CocoaPods Trunk
CocoaPods Trunk is an authentication and CocoaPods API service. To publish new or updated libraries to CocoaPods for public release you will need to be registered with Trunk and have a valid Trunk session on your current device. You can read about Trunk's history and development on the blog, and about private pods for yourself or your team.

使用你的email在當(dāng)前設(shè)備上注冊(cè)一個(gè)會(huì)話:

$ pod trunk register orta@cocoapods.org 'Orta Therox' --description='macbook air'

收到郵件之后,記得需要到郵箱里面激活下。當(dāng)然也可以通過(guò)以下命令來(lái)查案你電腦中trunk的會(huì)話信息。

pod trunk me

Deploying a library

  • Lints your Podspec locally

pod spec lint [NAME.podspec]

  • 成功之后就可以推送到CocoaPod
    如果推送到Public的使用

pod trunk push [NAME.podspec]
私有庫(kù)的話就推送到
pod repo push REPO [NAME.podspec]

  • 推送成功后,trunk將會(huì)發(fā)布一個(gè)權(quán)威的json代表陳述你的Podspec

添加其他人作為貢獻(xiàn)者
// For example, to add kyle@cocoapods.org to the library ARAnalytics:

$ pod trunk add-owner ARAnalytics kyle@cocoapods.org

八、Private Pods

Private Pods
CocoaPods is a great tool not only for adding open source code to your project, but also for sharing components across projects. You can use a private Spec Repo to do this.

具體步驟

    1. Create a Private Spec Repo
      You do not need to fork the CocoaPods/Specs Master repo. Make sure that everyone on your team has access to this repo, but it does not need to be public.
    1. Add your Private Repo to your CocoaPods installation

$ pod repo add REPO_NAME SOURCE_URL

To check if your installation is successful and ready to go:

$ cd ~/.cocoapods/repos/REPO_NAME
$ pod repo lint .
    1. Add your Podspec to your repo
      Make sure you've tagged and versioned your source appropriately, then run:

$ pod repo push REPO_NAME SPEC_NAME.podspec

This will run pod spec lint, and take care of all the little details for setting up the spec in your private repo.
The structure of your repo should mirror this:

.
├── Specs
    └── [SPEC_NAME]
        └── [VERSION]
            └── [SPEC_NAME].podspec

Your private Pod is ready to be used in a Podfile. You can use the spec repository with the source directive in your Podfile as shown in the following example:

source 'URL_TO_REPOSITORY'

如何移除私有庫(kù)

pod repo remove [name]

九、所有的命令匯總

十、上傳之后如何提高自己的庫(kù)搜索排名

附錄:

參考鏈接:

CocoaPods Guides

?著作權(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),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

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

  • 一、創(chuàng)建的github倉(cāng)庫(kù)原文地址 1 進(jìn)入Github網(wǎng)站www.github.com登陸自己的賬號(hào)后 2 建立...
    freesan44閱讀 4,127評(píng)論 2 12
  • 項(xiàng)目組件化、平臺(tái)化是技術(shù)公司的共同目標(biāo),越來(lái)越多的技術(shù)公司推崇使用pod管理第三方庫(kù)以及私有組件,一方面使項(xiàng)目架構(gòu)...
    swu_luo閱讀 22,873評(píng)論 0 39
  • rljs by sennchi Timeline of History Part One The Cognitiv...
    sennchi閱讀 7,872評(píng)論 0 10
  • 入秋未涼的海閱讀 112評(píng)論 0 0
  • 閑來(lái)無(wú)事,只能看書。 想起以前上小學(xué)的時(shí)候,一到周末,媽媽不放心我一個(gè)人在家,上班的時(shí)候就帶我一起,然后把我扔在店...
    甲小魚biu閱讀 373評(píng)論 0 2

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