序:
對於 Cocoapods,你是否還只知道 pod install,pod update,有時候還會摻雜上 pod search, pod setup等,對於它的工作流程都不是很熟悉,又或許你在搞組件化,但是對於pod庫的製作流程有些記不清,可以瀏覽瀏覽
說來慚愧,2015年底參加工作的時候,我的小組長告訴我有一個管理第三方的工具,啥配置都不用管,拉下來就用,升級一個命令就行...
離職之後在另外兩家公司都沒有使用,我對 Cocoapods 的感覺,好像什麼都知道,又好像什麼都不知道,跳槽到58後發現這邊在搞組件化用到了,重新整理一下
目錄
- 一、Cocopods流程及使用
- 二、pod庫的製作
- 三、pod私有庫的製作(一般用於組件化)
一、Cocopods流程及使用
作用:管理Xcode依賴的第三方框架
手動管理:
1、有可能會添加很多配置及系統依賴框架,繁瑣易錯;
2、如果框架升級了,需要替換本地工程裏面的框架,再重新配置。Cocopods管理:
快速、自動集成第三方框架,並編譯成一個libPod.a的靜態庫在項目中使用。
- 1.1、快速集成+簡單使用
// 1、初始化pod(會自動生成Podfile等文件)
- pod init
// 2、修改Podfile文件,添加需要的第三方
platform :ios, '9.0'
target 'PodUsageDemo' do
use_frameworks!
pod 'AFNetworking'
end
// 3、下載安裝
- pod install
- 1.2、Podfile文件
Podfile 是一種規範,描述了一個或多個 Xcode 項目的目標的依賴關係
// 1、根據不同的target下載不同的第三方代碼
target 'ProjectName' do
pod 'AFNetworking'
pod 'SDWebImage'
end
target 'NotificationService' do
pod 'GTExtensionSDK', '2.2.4'
end
// 2、版本控制
pod 'AFNetworking' # 使用最新版本
pod 'AFNetworking', '3.0.0' # 只使用3.0.0版本
除了沒有版本或特定版本之外,還可以使用邏輯運算符:
pod 'AFNetworking', '> 0.1' # 任何高於 0.1 的版本
pod 'AFNetworking', '>= 0.1' # 0.1 版及更高版本
pod 'AFNetworking', '< 0.1' # 任何低於 0.1 的版本
pod 'AFNetworking', '<= 0.1' # 版本 0.1 和任何更低版本
除了邏輯運算符之外,CocoaPods 還有一個樂觀運算符~>:
pod 'AFNetworking', '~> 0.1.2 ' # 0.1.2 及 0.2 及以下版本,不包括 0.2 及更高版本
pod 'AFNetworking', '~> 0.1' # 0.1 及 1.0 以下的版本,不包括 1.0 及更高版本
pod 'AFNetworking', '~> 0' # 版本 0 和 1.0 之前的版本,不包括 1.0 及更高版本
// 3、使用本地文件夾路徑(通常用於本地庫,一般建議採用相對路徑,避免重新配置)
pod 'AFNetworking', :path => '~/Documents/AFNetworking' # 絕對路徑
pod 'AFNetworking', :path => '../AFNetworking' # 相對路徑
// 4、使用遠程索引庫的地址
# 使用遠程地址
pod 'AFNetworking', :git => 'https://github.com/AFNetworking/AFNetworking.git'
# 使用遠程地址 + 'dev'分支
pod 'AFNetworking', :git => 'https://github.com/AFNetworking/AFNetworking.git', :branch => 'dev'
# 使用遠程地址 + 'tag'標籤
pod 'AFNetworking', :git => 'https://github.com/AFNetworking/AFNetworking.git', :tag => '3.1.1'
# 使用遠程地址 + 'commit'提交記錄
pod 'AFNetworking', :git => ''https://github.com/AFNetworking/AFNetworking.git', :commit => '0f506b1c45'
何時使用
pod install :添加、刪除pod的時候(即使工程中已經存在Podfile或者已經執行過pod install)
pod update [#PODNAME] :僅在要將pod 更新到較新版本時使用主要區別場景
在Podfile文件內的框架版本描述中,有沒有制定具體版本
pod --help
+ install Install project dependencies according to versions from a Podfile.lock
+ update Update outdated project dependencies and create new Podfile.lock
1、pod install
- 第一次在工程中使用pods時、每次編輯Podfile文件(新增、移除、更新)時使用;
- 每次執行pod install時,除了下載安裝,還會在Podfile.lock文件夾中生成版本信息,用於跟蹤和鎖定該版本;
- Podfile.lock,在第一次運行pod install後生成的,記錄每個安裝庫的版本號,並且鎖定了這些版本;
- 對於在Podfile.lock中記錄的版本,該命令會直接下載安裝,不會去檢查是否有新的版本;
- 對於不在Podfile.lock裏面的庫,執行該命令後會從Podfile文件的信息中查找對應版本。
2、pod update
- pod update參照Podfile安裝,如果Podfile中沒有做版本限制,會直接將第三方庫升級爲最新的
- 只關心Podfile中的版本限制,不考慮Podfile.lock裏面的限制
- 如果只運行pod update,後面沒有跟庫的名字,CocoaPods就會更新Podfile裏面所有的庫到最新版本
3、pod outdated
- 會列出所有較Podfile.lock裏面有新版本的庫(一般用於更新前檢查)
4、提升安裝速度
輸入pod install或者pod update之後,
- CocoaPods首先會去匹配本地spec庫;
- 在確認spec庫不需要更新之後,纔會下載相應的庫文件;
- 這樣比較耗時,可使用以下命令,跳過spec版本庫更新匹配
pod update --verbose --no-repo-update
pod install --verbose --no-repo-update
5、約定俗稱的規則
- 提交代碼時,省略Pod文件,其他人克隆源碼後直接執行pod install,可節省遠程倉庫存儲空間
- 一般添加pod的時候都會指明版本(防止其他人誤更新)
- 關於升級:除非組內一起升級,保持版本同步纔會執行pod update,否則只執行pod install
- 1.4、pod search xxxx
- 檢索第三方框架
- 內部做的事情:從本地緩存的‘第三方框架描述信息’(.json)生成的檢索文件中檢索到相關框架信息
- 常見問題:檢索不到,刪除cocopods索引文件,rm ~/Library/Cache/...search_index.json
- 搜索失敗及解決
[!] Unable to find a pod with name, author, summary, or description matchingLeeButtonLayout
1、刪除search_index.json
2、執行如下代碼
pod repo remove master
cd ~/.cocoapods/repos
git clone --depth 1 https://github.com/CocoaPods/Specs.git master
rm ~/Library/Caches/CocoaPods/search_index.json
pod search LeeButtonLayout
- 1.5、pod setup
將遠程索引庫克隆到本地索引庫
- 1.6、名詞解釋
- 遠程索引庫:https://github.com/CocoaPods/Specs
- 本地索引庫:/Users/qiangzhi/.cocoapods/repos/master/Specs
- 本地索引庫檢索文件:/Users/qiangzhi/Library/Caches/CocoaPods/search_index.json
第三方源碼,第二次安裝特別快,就是因爲已經緩存到本地了,位置(/Users/qiangzhi/Library/Caches/CocoaPods/Pods)
如果刪除search_index.json索引文件後,執行pod search xx 會重新生成search_index.json,需要幾分鐘
二、pod庫的製作(以製作LeeButtonLayout爲例)
製作步驟
- 從模版庫創建工程 (pod lib create LeeButtonLayout)
- 將代碼添加到 Classes 文件夾中
- 修改.podspec配置文件
- 將代碼上傳到GitHub遠程倉庫,並且打好標籤
- pod驗證
- 將.podspec文件發佈到公有庫中(trunk方式)
- 更新本地pod第三方框架信息數據源
- 2.1、從模版庫創建工程
// 創建一個文件夾(例:remoteLib),從終端進入,執行以下命令,LeeButtonLayout就是庫的名稱
pod lib create LeeButtonLayout
// 你想在什麼平臺使用??(由於是製作移動端的庫,所以輸入iOS)
What platform do you want to use?? [ iOS / macOS ]
> iOS
// 你想使用什麼語言??(我製作的庫是使用ObjectC開發,所以輸入ObjC)
What language do you want to use?? [ Swift / ObjC ]
> ObjC
// 您想在庫中包含演示應用程序嗎?(輸入Yes,製作完成時會自動創建一個Example文件,裏面是使用demo)
Would you like to include a demo application with your library? [ Yes / No ]
> Yes
// 您將使用哪些測試框架?(暫無)
Which testing frameworks will you use? [ Specta / Kiwi / None ]
> None
// 你想做基於視圖的測試嗎?(暫無)
Would you like to do view based testing? [ Yes / No ]
> No
// 類前綴
What is your class prefix?
> Lee
⚠️過程如下圖所示
回到Example文件夾下,執行pod install,進行代碼測試
- 2.2、修改.podspec配置文件
- 2.3、將代碼上傳到GitHub遠程倉庫,並且打好標籤
// 1、在github上創建好遠程倉庫,拿到遠程倉庫地址
// 2、初始化git、並關聯推送到遠程倉庫
qiangzhi@qiangzhideMacBook-Pro LeeButtonLayout % git init
qiangzhi@qiangzhideMacBook-Pro LeeButtonLayout % git add .
qiangzhi@qiangzhideMacBook-Pro LeeButtonLayout % git commit -m "first commit"
qiangzhi@qiangzhideMacBook-Pro LeeButtonLayout % git branch -M master
qiangzhi@qiangzhideMacBook-Pro LeeButtonLayout % git remote add origin https://github.com/leeqiangzi/LeeButtonLayout.git
qiangzhi@qiangzhideMacBook-Pro LeeButtonLayout % git push -u origin master
// 3、添加tag並推送到遠程倉庫
qiangzhi@qiangzhideMacBook-Pro LeeButtonLayout % git tag '0.1.0'
qiangzhi@qiangzhideMacBook-Pro LeeButtonLayout % git push --tags
⚠️GitHub密碼驗證的支持在2021年8月13日之後就不支持了,需要配置ssh/token,可參考-《筆記-關於GitHub的操作》
- 2.4、pod驗證(本地+遠程)
// 驗證本地庫
qiangzhi@qiangzhideMacBook-Pro LeeButtonLayout % pod lib lint
// 驗證遠程庫
qiangzhi@qiangzhideMacBook-Pro LeeButtonLayout % pod spec lint
二者主要區別:本地驗證,不會驗證tag
// 1、註冊賬戶(郵箱+姓名+描述)
- pod trunk register [email protected] 'leeqiangzi' --description='LeeButtonLayout的配置文件'
// 2、打開郵箱,通過郵件內容驗證section
// 3、推送到遠程
- pod trunk push LeeButtonLayout.podspec
執行結果
// 執行打印結果
Updating spec repo `trunk` // 這一步在更新本地索引庫
Validating podspec
-> LeeButtonLayout (0.1.0)
- NOTE | xcodebuild: note: Using new build system
- NOTE | xcodebuild: note: Using codesigning identity override: -
- NOTE | xcodebuild: note: Build preparation complete
- NOTE | [iOS] xcodebuild: note: Planning
- NOTE | [iOS] xcodebuild: note: Building targets in dependency order
Updating spec repo `trunk`
--------------------------------------------------------------------------------
🎉 Congrats
🚀 LeeButtonLayout (0.1.0) successfully published
📅 January 31st, 05:38
🌎 https://cocoapods.org/pods/LeeButtonLayout
👍 Tell your friends!
--------------------------------------------------------------------------------
查看自己是否註冊過trunk
qiangzhi@qiangzhideMacBook-Pro LeeButtonLayout % pod trunk me
- Name: leeqiangzi
- Email: [email protected]
- Since: January 31st, 05:33
- Pods:
- LeeButtonLayout
- Sessions:
- January 31st, 05:33 - June 8th, 18:59. IP: 43.239.85.131 Description:
LeeButtonLayout的配置文件
- 2.6、pod升級(子庫拆分+依賴)
pod升級過程
1、修改升級代碼,提交到遠程倉庫,更新tag
2、修改spec文件,trunk方式提交
// 升級結果
qiangzhi@qiangzhideMacBook-Pro LeeButtonLayout % pod trunk push LeeButtonLayout.podspec
Updating spec repo `trunk`
Validating podspec
-> LeeButtonLayout (0.2.0)
- NOTE | xcodebuild: note: Using new build system
- NOTE | xcodebuild: note: Using codesigning identity override: -
- NOTE | xcodebuild: note: Build preparation complete
- NOTE | [iOS] xcodebuild: note: Planning
- NOTE | [iOS] xcodebuild: note: Building targets in dependency order
- NOTE | [iOS] xcodebuild: Pods.xcodeproj: warning: The iOS Simulator deployment target 'IPHONEOS_DEPLOYMENT_TARGET' is set to 8.0, but the range of supported deployment target versions is 9.0 to 15.2.99. (in target 'AFNetworking' from project 'Pods')
- NOTE | [iOS] xcodebuild: Pods.xcodeproj: warning: MobileCoreServices has been renamed. Use CoreServices instead. (in target 'AFNetworking' from project 'Pods')
Updating spec repo `trunk`
--------------------------------------------------------------------------------
🎉 Congrats
🚀 LeeButtonLayout (0.2.0) successfully published
📅 February 6th, 03:40
🌎 https://cocoapods.org/pods/LeeButtonLayout
👍 Tell your friends!
--------------------------------------------------------------------------------
三、遠程私有庫
與發佈到pod上的主要區別
1、需要兩個遠程倉庫(代碼倉庫 + spec倉庫)
2、需要配置源路徑,Podfile需要添加 source 'xxx'
3、推送配置文件使用pod repo push xxx xxx.podspec,不再使用trunk方式其他步驟與 ‘二、pod庫的製作’ 基本一致,此方法一般用於公司內部組件化開發使用
- 3.1、創建遠程代碼倉庫 + 遠程配置倉庫
- 3.2、從模版庫創建工程
- 3.3、將代碼添加到Classes文件中
- 3.4、在Example文件夾下執行pod install,進行代碼編譯測試
-
3.5、修改spec配置文件
- 3.6、將代碼提交到遠程倉庫,添加tag
// 初始化git、關聯遠程倉庫、推送代碼
qiangzhi@qiangzhideMacBook-Pro LeeLimitInput % git init
qiangzhi@qiangzhideMacBook-Pro LeeLimitInput % git add .
qiangzhi@qiangzhideMacBook-Pro LeeLimitInput % git commit -m 'first commit'
qiangzhi@qiangzhideMacBook-Pro LeeLimitInput % git remote add origin https://e.coding.net/leeqiangzi/PrivatePods/LeeLimitInput.git
qiangzhi@qiangzhideMacBook-Pro LeeLimitInput % git push origin master
// 添加標籤並推送
qiangzhi@qiangzhideMacBook-Pro LeeLimitInput % git tag '0.1.0'
qiangzhi@qiangzhideMacBook-Pro LeeLimitInput % git push --tags
- 3.7、驗證(本地+遠程)
- 3.8、推送配置文件
1、添加源地址
pod repo add LeeLimitInput https://e.coding.net/leeqiangzi/PrivatePods/LeeLimitInputSpecs.git
2、將spec推送到遠程
pod repo push LeeLimitInput LeeLimitInput.podspec
⚠️如果名稱寫錯可通過 pod repo remove xxx 刪除
- 3.9、修改Podfile(添加source),執行pod install
# 如果不添加source,則直接到pod的master中查找
# 通過pod repo查看source
source 'https://e.coding.net/leeqiangzi/PrivatePods/LeeLimitInputSpecs.git'
# 公有庫源添加
source 'https://github.com/CocoaPods/Specs.git'
platform :ios, '9.0'
target 'PodUsageDemo' do
use_frameworks!
pod 'LeeLimitInput'
end