CocoaPods 已經(jīng)出現(xiàn)很多年了,相信很多同學都會使用,但是你真的知道 CocoaPods 是如何工作的嗎?
RubyGems
The RubyGems software allows you to easily download, install, and use ruby software packages on your system. The software package is called a “gem” which contains a packaged Ruby application or library.
人們特別是電腦工程師們,常常從機器著想。他們認為:“這樣做,機器就能運行的更快;這樣做,機器運行效率更高;這樣做,機器就會怎樣怎樣怎樣?!睂嶋H上,我們需要從人的角度考慮問題,人們怎樣編寫程序或者怎樣使用機器上應用程序。我們是主人,他們是仆人 ———— Ruby設計初衷
CocoaPods 是使用 Ruby 語言編寫的一個作為 iOS 的包管理工具,而 RubyGems 是 Ruby 的包管理工具。安裝 CocoaPods 需要包管理工具 RubyGems 安裝,而 RubyGems 是 Mac 自帶的工具。關于RubyGems的更多信息,可以參考 官方文檔 。不過,RubyGems 中的 Gems 在國內(nèi)訪問速度很慢,不過可以使用 Ruby China。如果你想大體了解 Ruby,可以看這篇文章 Ruby。讀到這里相信你對安裝 CocoaPods 已經(jīng)有了一定的了解。
CocoaPods 會被安裝到 /Users/wangsuyan/.cocoapods/repos
更新到最新版本:sudo gem install cocoapods
安裝指定版本: sudo gem install cocoapods -v 1.3.1
Git
掌握 CocoaPods ,需要了解一些 Git 的基本知識,當然你需要至少有一個代碼托管平臺,比如 GitHub。當然 這篇文章 講的很不錯。下面主要說明與這篇文章相關的內(nèi)容。
git add -A
把文提交到暫存區(qū),等待提交git commit -m "代碼提交信息"
提交代碼到 HEAD,現(xiàn)在,你的改動已經(jīng)提交到了 HEAD,但是還沒到你的遠端倉庫git tag 1.0
給當前要提交的版本打個標簽git push --tags
提交所有的 tag 到遠端倉庫
Pod init
它首先需要判斷當前目錄有沒有 XCODEPROJ 項目,如果沒有直接報錯;若果有單個 XCODEPROJ 項目,會直接創(chuàng)建一個 podfile 文件;如果有多個XCODEPROJ 項目,需要指定一個項目,否則會報錯:
[!] Multiple Xcode projects found, please specify one
Podfile
經(jīng)過 Pod init 后會生成一個 Podfile 文件。它是一種規(guī)范,描述了一個或多個Xcode項目(target)的依賴關系。
最簡單的 Podfile 文件,它僅僅給 Target lefeKit 添加一個 SDWebImage 庫。
platform :ios, '9.0'
target 'lefeKit' do
pod 'SDWebImage'
end
use_frameworks!
使用 Swift 或者動態(tài)庫時需要
- 版本,比如:
pod 'SDWebImage', '~> 3.7.0'
假如 SDWebImage 目前只有下列版本:
4.1.0, 4.0.0, 4.0.0-beta2, 4.0.0-beta,
3.8.2, 3.8.1, 3.8.0, 3.7.6, 3.7.5, 3.7.4, 3.7.3, 3.7.2, 3.7.1, 3.7.0, 3.6, 3.5.4, 3.5.2, 3.5.1, 3.5,3.4, 3.3, 3.2, 3.1, 3.0,
2.7.4, 2.7, 2.6, 2.5, 2.4
那么 pod 'SDWebImage', '~> 3.7.0' 只會安裝 3.7.6 版本,也就是最后一個 . 的最高版本;
pod 'SDWebImage', '> 3.7.0' 安裝大于 3.7.0 版本,當然有 >=, <= 和 <
pod 'SDWebImage', '3.7.6' 指定版本為 3.7.6
-
:path指定本地的 Pod 庫
pod 'FLoatDemo', :path => '~/Desktop/TestDemo/FLoatDemo'
這里的地址不是隨便一個目錄就可以,必須是一個 Pod 庫,不然會報錯
No podspec found for `FLoatDemo` in `~/Desktop/TestDemo/FLoatDemo`
- 指定來源
如果第三方庫不能滿足您項目的需求,那么你可以 Fork一份,來修改第三方代碼。
pod 'SDWebImage', :git => 'https://github.com/lefex/SDWebImage.git', :commit => '94cdb773d74967f7ba2feecf0d151012bd965fde'
還可以有::branch, :tag => '3.1.1'
- Subspecs
某個庫可能很大,但是你僅僅需要某一部分,你只需要導入你需要的那部分即可,比如:

如何我們只需要 pod ‘SDWebImage/Core’
也可以:pod 'SDWebImage', :subspecs => ['Core', 'GIF']
- 去除警告
去除全部警告
inhibit_all_warnings!
去除某個庫的警告
pod 'SDWebImage', '~> 4.1.0', :inhibit_warnings => true
pod install
如果修改了 Podfile 文件,那么執(zhí)行 pod install。執(zhí)行 pod install 后,會根據(jù) Podfile 中的描述來安裝所依賴的庫。這時會生成很多文件。
- Podfile.lock
這個文件主要用來鎖定 Pods 庫的版本。保證組內(nèi)成員所使用的三方庫都是統(tǒng)一版本。如果處理不當,這里很可能發(fā)生沖突,而且比較嚴重。
- Manifest.lock
是 Podfile.lock 的副本,每次只要生成 Podfile.lock 時就會生成一個一樣的 Manifest.lock 存儲在 Pods 文件夾下。在每次項目 Build 的時候,會跑一下腳本檢查一下 Podfile.lock 和 Manifest.lock 是否一致,如果不一致就拋出異常。這是它的腳本。
diff "${PODS_PODFILE_DIR_PATH}/Podfile.lock" "${PODS_ROOT}/Manifest.lock" > /dev/null
if [ $? != 0 ] ; then
# print error to STDERR
echo "error: The sandbox is not in sync with the Podfile.lock. Run 'pod install' or update your CocoaPods installation." >&2
exit 1
fi
# This output is used by Xcode 'outputs' to avoid re-running this script phase.
echo "SUCCESS" > "${SCRIPT_OUTPUT_FILE_0}"
pod update
僅僅把 Pods 更新到新的版本時需要。比如 lefeKit 項目中當前 SDWebImage 的版本為 3.7.0,這時我修改 Podfile 文件為:pod 'SDWebImage', '>3.6.0',執(zhí)行 pod install 后,本地的 SDWebImage 的版本任然為 3.7.0 。當執(zhí)行 pod update 后變?yōu)?4.1.0
發(fā)布一個 Pod 庫
創(chuàng)建
下面以 lefeKit 為例,說明創(chuàng)建私有庫的過程。
- pod lib create lefeKit
這時需要輸入提示問題,按照官方文檔 逐步完成; - 登錄自己的 github,創(chuàng)建一個名叫
lefeKit的項目; - 修改
lefeKit.podspec文件,
s.source = { :git => 'https://github.com/lefex/lefeKit.git', :tag => s.version.to_s }需要是你在 github 上創(chuàng)建的項目地址;s.homepage = 'https://github.com/lefex/lefeKit' - 根目錄下(lefeKit)創(chuàng)建目錄
Classes -
pod lib lint檢查lefeKit.podspec文件是否有錯,成功則顯示lefeKit passed validation; - 一切無誤后,執(zhí)行
pod trunk push lefeKit.podspec -
pod trunk me可以查看我注冊的信息
這些步驟不是所有的都有先后順序,創(chuàng)建私有庫,關鍵是創(chuàng)建 xxx.podspec 文件和一個 Repository,讓 xxx.podspec 關聯(lián)到 Repository。
發(fā)布成功的提示為:
--------------------------------------------------------------------------------
?? Congrats
?? lefeKit (1.0.0) successfully published
?? August 28th, 21:58
?? https://cocoapods.org/pods/lefeKit
?? Tell your friends!
--------------------------------------------------------------------------------
更新私有庫
- 修改
lefeKit.podspec文件中的版本號; - tag tat 1.0.0,添加一個 tag;
- git push --tags
pod lib lintpod trunk push lefeKit.podspec
錯誤總結
Authentication token is invalid or unverified. Either verify it with the email that was sent or register a new session.
遇到這個錯誤說明你還沒有注冊賬號,注冊一個郵箱 pod trunk register wsyxyxs@126.com
WARN | url: The URL (https://github.com/lefex1/lefeKit) is not reachable
確保地址可以正確訪問 https://github.com/lefex1/lefeKit 應為 https://github.com/lefex1/lefeKit
ERROR | [iOS] file patterns: The `source_files` pattern did not match any file.
找不到資源文件,在根目錄下創(chuàng)建 Classes 文件夾,并創(chuàng)建文件。s.source_files = 'lefeKit/Classes/**/*'
Unable to find a pod with name, author, summary, or description matching `lefeKit`
這個是本地緩存的問題:
清理緩存 rm -rf ~/Library/Caches/Cocoapods,執(zhí)行 pod setup
技巧
所有命令后添加 --verbose,會顯示更多的調(diào)試信息。
推薦閱讀
【iOS 國際化】如何把國際化時需要3天的工作量縮減到10分鐘
Promise
微信iOS數(shù)據(jù)庫是什么樣的
感謝
===== 我是有底線的 ======
喜歡我的文章,歡迎關注我的新浪微博 Lefe_x,我會不定期的分享一些開發(fā)技巧
