本文旨在解决如何把自己的库生成Pod供别人使用。网上文章很多,坑也很多。既然这样,我就自己总结下供大家参考学习。
第一步:Cocoapods的使用
pod install
pod update
在开发中相信大家一定会使用了,在此就不再叙述了。
第二步:向Cocoapods注册Trunk
CocoaPods Trunk是什么
CocoaPods Trunk是CocoaPods的官方发布服务。注册Trunk主要用于将自己开发的CocoaPods库发布到公共的CocoaPods仓库中,方便其他开发者使用。
注册CocoaPods Trunk 仓库
pod trunk register [Your-Email] '[Your-Name]' --description='[Your-Desc]'
[Your-Email]: 任意邮件,但是我比较推荐你使用github上的Email
[Your-Name]: 推荐使用github上使用的Name
[Your-Desc]: 一个简单的描述,往往这个时候我们使用的是自己电脑的一个描述
// 例子
pod trunk register songjinfeng2@163.com 'jfcustomnavigation' --description='在CocoaPods Trunk上的代码仓库'
// 注册完成后,你可以通过 pod trunk me 命令查看信息
pod trunk me
pod trunk me 执行结果
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注册Trunk注册服务完成,待后续使用。
第三步:创建开源的代码仓库
由于gitee使用更加方便,这里我使用gitee来举例说明
1、首先在gitee上建一个名字为 jfcustomnavigation 的代码仓库:如图
20250114192407.jpg
2、把空白仓库clone到本地
git clone https://gitee.com/jeremy987654321/jfcustomnavigation.git
克隆完成后进入仓库目录
cd ../jfcustomnavigation
3、创建 podspec 文件并将代码或者资源添加到项目中
pod spec create [NAME]
> [NAME]: podspec 名称,一般与你在git上创建的repository相同
// 如:
pod spec create jfcustomnavigation
执行 pod spec create jfcustomnavigation 命令后的效果图如下
1736855060581.jpg
4、修改.podspec
podspec书写规范(https://www.nowcoder.com/discuss/518600313205534720)
Pod::Spec.new do |s|
s.name = "jfcustomnavigation"
s.version = "0.0.1"
s.summary = "OC代码的自定义导航栏,用前需要主动隐藏系统导航栏"
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、提交代码仓库
#add 并 commit 代码
git add -A && git commit -m "Release 0.0.1"
#给代码设置tag版本号
git tag '0.0.1'
#提交 tag版本号
git push --tags
#提交到 代码仓库
git push origin master
特别注意:
版本号中一定不能带引号哦,如下图‘1.0.2’是错误的。
image.png
6、 pod 验证
执行以下命令,为 Pod
添加版本号,并打上 tag
,这个tag要与代码仓库中的tag相对应:
#为 Pod 添加版本号
set the new version to 0.0.1
#为 Pod 添加tag,这个tag要与代码仓库中的tag相对应
set the new tag to 0.0.1
接下来是验证
pod lib lint
用这条命令可能会出现各种错误
建议使用
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
如果一切正常,终端中会输出:
jfcustomnavigation (0.0.1)
jfcustomnavigation passed validation.
到此,pod 验证就成功。
7、部署你的Pod
以上工作都就绪后,就可以将我们的 Pod
提交给 CocoaPods
了,CocoaPods 使用 trun
服务让我们来提交 Pod
,也就是把这个版本信息提交到pod仓库里,我们再去pod search jfcustomnavigation时就能够搜到相应的版本了。
使用以下命令,通过 trunk
部署你的 Pod
:
pod trunk push jfcustomnavigation.podspec
如何用上边命令一直报错建议使用下边的命令
pod trunk push jfcustomnavigation.podspec --allow-warnings --use-libraries --skip-import-validation
8、搜索jfcustomnavigation库,验证是否成功
// 先更新一下repo
$ pod repo update
// 查找一下你提交的pod
pod search 'jfcustomnavigation'
20250114232616.jpg
tips: 如果你在pod search无法找到你的pod,可以参照以下步骤:
执行 pod repo update 后重新pod search
或者:
pod setup 然后删除 rm ~/Library/Caches/CocoaPods/search_index.json 再重新pod search