本文旨在解決如何把自己的庫(kù)生成Pod供別人使用。網(wǎng)上文章很多,坑也很多。既然這樣,我就自己總結(jié)下供大家參考學(xué)習(xí)。
第一步:Cocoapods的使用
pod install
pod update
在開(kāi)發(fā)中相信大家一定會(huì)使用了,在此就不再敘述了。
第二步:向Cocoapods注冊(cè)Trunk
CocoaPods Trunk是什么
CocoaPods Trunk是CocoaPods的官方發(fā)布服務(wù)。注冊(cè)Trunk主要用于將自己開(kāi)發(fā)的CocoaPods庫(kù)發(fā)布到公共的CocoaPods倉(cāng)庫(kù)中,方便其他開(kāi)發(fā)者使用。
注冊(cè)CocoaPods Trunk 倉(cāng)庫(kù)
pod trunk register [Your-Email] '[Your-Name]' --description='[Your-Desc]'
[Your-Email]: 任意郵件,但是我比較推薦你使用github上的Email
[Your-Name]: 推薦使用github上使用的Name
[Your-Desc]: 一個(gè)簡(jiǎn)單的描述,往往這個(gè)時(shí)候我們使用的是自己電腦的一個(gè)描述
// 例子
pod trunk register songjinfeng2@163.com 'jfcustomnavigation' --description='在CocoaPods Trunk上的代碼倉(cāng)庫(kù)'
// 注冊(cè)完成后,你可以通過(guò) pod trunk me 命令查看信息
pod trunk me
pod trunk me 執(zhí)行結(jié)果
devin@songjinfengdeMacBook-Pro ~ % pod trunk me
- Name: OnlyJeremy
- Email: songjinfeng2@163.com
- Since: January 13th, 08:33
- Pods:
- jfcustomnavigation
- Sessions:
- January 13th, 08:33 - May 22nd, 02:14. IP: 120.245.115.13
Description: songjinfeng Mac Pro 14
到此第二步,向Cocoapods注冊(cè)Trunk注冊(cè)服務(wù)完成,待后續(xù)使用。
第三步:創(chuàng)建開(kāi)源的代碼倉(cāng)庫(kù)
由于gitee使用更加方便,這里我使用gitee來(lái)舉例說(shuō)明
1、首先在gitee上建一個(gè)名字為 jfcustomnavigation 的代碼倉(cāng)庫(kù):如圖

2、把空白倉(cāng)庫(kù)clone到本地
git clone https://gitee.com/jeremy987654321/jfcustomnavigation.git
克隆完成后進(jìn)入倉(cāng)庫(kù)目錄
cd ../jfcustomnavigation
3、創(chuàng)建 podspec 文件并將代碼或者資源添加到項(xiàng)目中
pod spec create [NAME]
> [NAME]: podspec 名稱,一般與你在git上創(chuàng)建的repository相同
// 如:
pod spec create jfcustomnavigation
執(zhí)行 pod spec create jfcustomnavigation 命令后的效果圖如下

4、修改.podspec
podspec書寫規(guī)范(https://www.nowcoder.com/discuss/518600313205534720)
Pod::Spec.new do |s|
s.name = "jfcustomnavigation"
s.version = "0.0.1"
s.summary = "OC代碼的自定義導(dǎo)航欄,用前需要主動(dòng)隱藏系統(tǒng)導(dǎo)航欄"
s.homepage = "https://gitee.com/jeremy987654321/jfcustomnavigation"
#s.license = "MIT"
s.license = { :type => "MIT", :file => "LICENSE" }
s.author = { "songjinfeng" => "songjinfeng2@163.com" }
s.platform = :iOS
s.platform = :ios, "7.0"
s.source = { :git => "https://gitee.com/jeremy987654321/jfcustomnavigation.git", :tag => "#{s.version}" }
s.source_files = "jfcustomnavigation", "jfcustomnavigation/**/*.{h,m}"
s.frameworks = "Foundation", "UIKit"
#s.dependency 'LixMacro', '~> 0.0.3'
end
5、提交代碼倉(cāng)庫(kù)
#add 并 commit 代碼
git add -A && git commit -m "Release 0.0.1"
#給代碼設(shè)置tag版本號(hào)
git tag '0.0.1'
#提交 tag版本號(hào)
git push --tags
#提交到 代碼倉(cāng)庫(kù)
git push origin master
特別注意:
版本號(hào)中一定不能帶引號(hào)哦,如下圖‘1.0.2’是錯(cuò)誤的。

6、 pod 驗(yàn)證
執(zhí)行以下命令,為 Pod 添加版本號(hào),并打上 tag,這個(gè)tag要與代碼倉(cāng)庫(kù)中的tag相對(duì)應(yīng):
#為 Pod 添加版本號(hào)
set the new version to 0.0.1
#為 Pod 添加tag,這個(gè)tag要與代碼倉(cāng)庫(kù)中的tag相對(duì)應(yīng)
set the new tag to 0.0.1
接下來(lái)是驗(yàn)證
pod lib lint
用這條命令可能會(huì)出現(xiàn)各種錯(cuò)誤
建議使用
pod lib lint jfcustomnavigation.podspec --allow-warnings --sources='https://github.com/CocoaPods/Specs.git,https://gitee.com/jeremy987654321/jfcustomnavigation.git' --use-libraries --no-clean --verbose --skip-import-validation
如果一切正常,終端中會(huì)輸出:
jfcustomnavigation (0.0.1)
jfcustomnavigation passed validation.
到此,pod 驗(yàn)證就成功。
7、部署你的Pod
以上工作都就緒后,就可以將我們的 Pod 提交給 CocoaPods 了,CocoaPods 使用 trun 服務(wù)讓我們來(lái)提交 Pod,也就是把這個(gè)版本信息提交到pod倉(cāng)庫(kù)里,我們?cè)偃od search jfcustomnavigation時(shí)就能夠搜到相應(yīng)的版本了。
使用以下命令,通過(guò) trunk 部署你的 Pod:
pod trunk push jfcustomnavigation.podspec
如何用上邊命令一直報(bào)錯(cuò)建議使用下邊的命令
pod trunk push jfcustomnavigation.podspec --allow-warnings --use-libraries --skip-import-validation
8、搜索jfcustomnavigation庫(kù),驗(yàn)證是否成功
// 先更新一下repo
$ pod repo update
// 查找一下你提交的pod
pod search 'jfcustomnavigation'

tips: 如果你在pod search無(wú)法找到你的pod,可以參照以下步驟:
執(zhí)行 pod repo update 后重新pod search
或者:
pod setup 然后刪除 rm ~/Library/Caches/CocoaPods/search_index.json 再重新pod search