引言:What? 搞iOS不知道CocoaPod
首先給出一個鏈接,通篇文章將完全是參照闻妓,官網(wǎng)的內(nèi)容進(jìn)行整理和總結(jié),也建議大家閱讀官網(wǎng)的文章,網(wǎng)上文件大多尋章摘句激涤。
CocoaPod官網(wǎng)的使用指南
CocoaPod官網(wǎng)
以上兩個鏈接,可謂學(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.
概括起來可以說明為是 CocoaPod通過Podfile文件(一種通過簡單文本描述來指定依賴庫的文件)可以解決類庫依賴倦踢,同時通過workSpace的形式添加到你的Xcode工程中。
其中繼目的就是建立一個更加中心化的生態(tài)系統(tǒng)來提升第三方庫管理侠草。
二辱挥、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
任何安裝問題可以點開該文章的鏈接,其中CocoaPod依賴于Ruby
三边涕、Using CocoaPods
生成一個CocoaPods
$ pod init
具體的Podfile語法
platform :ios, '9.0'
target 'MyApp' do
pod 'ObjectiveSugar'
end
之后的步驟
$ pod install
四晤碘、pod install vs. pod update
bundler, RubyGems or composer也有對對應(yīng)的語言,CocoaPod設(shè)計的和他們類似
pod install
適用于獲取pod,當(dāng)你編輯Podfile中存在add, update, remove a pod的需求的時候功蜓,可以使用該命令园爷。
- 根據(jù)Podfile.lock的內(nèi)容,搞定不在Podfile.lock里面的pods的依賴式撼,不會去嘗試檢查更新的版本童社。
- 對于不存在Podfile.lock里面的pod,將按照podfile里面的內(nèi)容進(jìn)行搜索對應(yīng)的版本
pod outdated
CocoaPods將羅列出在Podfile.lock擁有更新版本的pod,也就說如果你執(zhí)行pod update PODNAME,如果新的版本依然符合Podfile里面的描述端衰,那么將會被更新
pod update
兩種形式叠洗,pod update PODNAME將更新指定的pod,否則將更新所有。前提是符合Podfile里面的描述內(nèi)容
注意
基于podfile.lock的作用旅东,所以當(dāng)倉庫共享的時候灭抑,需要注意要提交podfile.lock。以方便統(tǒng)一的來鎖定依賴庫抵代。
五腾节、The Podfile
文件整體框架
簡單的例子
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
如果想多個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
具體的語法細(xì)則
指定版本規(guī)則
不指定版本
pod 'SSZipArchive'
凍結(jié)版本
pod 'Objection', '0.9'
邏輯運算符指定
- '> 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
~>來指定
- '~> 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'
指定對應(yīng)的git倉庫
默認(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'
指定對應(yīng)commit
pod 'Alamofire', :git => 'https://github.com/Alamofire/Alamofire.git', :commit => '0f506b1c45'
六、Making a CocoaPod
簡單的建立CocoaPod可以參考以上連接,細(xì)化請看如下說明案腺。
Using Pod Lib Create
pod lib create SLLibrary
默認(rèn)的模板如下圖庆冕,當(dāng)然我們也可以采用自己的模板,通過增加如下參數(shù)為--template-url=URL
七劈榨、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è)備上注冊一個會話:
$ pod trunk register orta@cocoapods.org 'Orta Therox' --description='macbook air'
收到郵件之后访递,記得需要到郵箱里面激活下。當(dāng)然也可以通過以下命令來查案你電腦中trunk的會話信息同辣。
pod trunk me
Deploying a library
- Lints your Podspec locally
pod spec lint [NAME.podspec]
- 成功之后就可以推送到CocoaPod
如果推送到Public的使用
pod trunk push [NAME.podspec]
私有庫的話就推送到
pod repo push REPO [NAME.podspec]
- 推送成功后拷姿,trunk將會發(fā)布一個權(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.
具體步驟
- 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.
- Create a Private Spec Repo
- 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 .
- Add your Podspec to your repo
Make sure you've tagged and versioned your source appropriately, then run:
- Add your Podspec to your repo
$ 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'
如何移除私有庫
pod repo remove [name]