每年三月份搞创新项目都要招一批人,这里整理下私有仓库的创建和发布方法
一、前期准备
-
确保环境正确
pod --version # 确认 CocoaPods 已安装 git --version # 确认 Git 已安装 -
注册 CocoaPods Trunk(首次发布需执行)
pod trunk register your_email@example.com "Your Name" --description="MacBook Pro"- 检查邮箱中的验证链接,验证后确认状态:
pod trunk me
- 检查邮箱中的验证链接,验证后确认状态:
二、创建库项目
-
使用模板生成项目
pod lib create YourLibName # 交互式选择配置(选 Swift/ObjC、示例工程等) cd YourLibName -
替换默认代码
- 将你的代码文件放入
YourLibName/Classes/目录。 - 删除模板生成的
ReplaceMe.swift。
- 将你的代码文件放入
三、配置 .podspec 文件
-
编辑关键字段(以
YourLibName.podspec为例)Pod::Spec.new do |s| s.name = 'YourLibName' s.version = '0.1.0' s.summary = 'A short description of YourLibName.' s.description = <<-DESC A detailed description of YourLibName. DESC s.homepage = 'https://github.com/YourGitHub/YourLibName' s.license = { :type => 'MIT', :file => 'LICENSE' } s.author = { 'YourName' => 'your_email@example.com' } s.source = { :git => 'https://github.com/YourGitHub/YourLibName.git', :tag => s.version.to_s } s.ios.deployment_target = '10.0' s.source_files = 'YourLibName/Classes/**/*.swift' # 根据实际路径调整 # s.dependency 'Alamofire' # 添加依赖库(可选) end -
验证配置合法性
pod lib lint --allow-warnings- 如果失败,根据错误提示修正(如路径错误、描述缺失等)。
四、关联 Git 仓库
-
本地初始化 Git
git init git add . git commit -m "Initial commit" -
关联远程仓库
git remote add origin https://github.com/YourGitHub/YourLibName.git git push -u origin main -
打版本标签
git tag 0.1.0 git push origin --tags
五、发布到 CocoaPods
-
正式发布
pod trunk push YourLibName.podspec- 成功后会显示:
🎉 Congrats! YourLibName (0.1.0) successfully published.
- 成功后会显示:
-
验证发布结果
- 等待 10-30 分钟后搜索:
pod search YourLibName - 或访问:https://cocoapods.org/pods/YourLibName
- 等待 10-30 分钟后搜索:
六、其他开发者安装
在 Podfile 中添加:
pod 'YourLibName'
然后执行:
pod install
如果刚刚发布,需要更新一下源,然后安装
pod repo update
pod install
七、更新库版本
-
修改
.podspec版本号(如0.1.0→0.1.1)。 -
提交代码并打新标签:
git tag 0.1.1 git push origin --tags -
重新发布:
pod trunk push YourLibName.podspec
八、常见问题处理
| 问题 | 解决方案 |
|---|---|
pod lib lint 失败 |
检查 s.source_files 路径是否匹配实际代码位置 |
[!] Unable to accept duplicate entry |
升级版本号并重新发布 |
pod search 找不到库 |
等待索引更新或运行 pod repo update
|
| 私有库依赖 | 在 Podfile 中添加 source 'https://github.com/your/private_repo.git'
|
九、关键注意事项
-
版本号唯一:每次发布必须递增
s.version。 -
Git 标签同步:
s.version必须与 Git 标签严格一致。 -
代码规范:确保代码在
Classes/目录下,且无编译警告。
按此流程操作,你的 CocoaPods 库即可被全球开发者使用!