cocoapod仓库创建与发布

每年三月份搞创新项目都要招一批人,这里整理下私有仓库的创建和发布方法


一、前期准备

  1. 确保环境正确

    pod --version           # 确认 CocoaPods 已安装
    git --version           # 确认 Git 已安装
    
  2. 注册 CocoaPods Trunk(首次发布需执行)

    pod trunk register your_email@example.com "Your Name" --description="MacBook Pro"
    
    • 检查邮箱中的验证链接,验证后确认状态:
      pod trunk me
      

二、创建库项目

  1. 使用模板生成项目

    pod lib create YourLibName  # 交互式选择配置(选 Swift/ObjC、示例工程等)
    cd YourLibName
    
  2. 替换默认代码

    • 将你的代码文件放入 YourLibName/Classes/ 目录。
    • 删除模板生成的 ReplaceMe.swift

三、配置 .podspec 文件

  1. 编辑关键字段(以 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
    
  2. 验证配置合法性

    pod lib lint --allow-warnings
    
    • 如果失败,根据错误提示修正(如路径错误、描述缺失等)。

四、关联 Git 仓库

  1. 本地初始化 Git

    git init
    git add .
    git commit -m "Initial commit"
    
  2. 关联远程仓库

    git remote add origin https://github.com/YourGitHub/YourLibName.git
    git push -u origin main
    
  3. 打版本标签

    git tag 0.1.0
    git push origin --tags
    

五、发布到 CocoaPods

  1. 正式发布

    pod trunk push YourLibName.podspec
    
    • 成功后会显示:
      🎉  Congrats! YourLibName (0.1.0) successfully published.
      
  2. 验证发布结果


六、其他开发者安装

Podfile 中添加:

pod 'YourLibName'

然后执行:

pod install

如果刚刚发布,需要更新一下源,然后安装

pod repo update
pod install

七、更新库版本

  1. 修改 .podspec 版本号(如 0.1.00.1.1)。
  2. 提交代码并打新标签
    git tag 0.1.1
    git push origin --tags
    
  3. 重新发布
    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'

九、关键注意事项

  1. 版本号唯一:每次发布必须递增 s.version
  2. Git 标签同步s.version 必须与 Git 标签严格一致。
  3. 代码规范:确保代码在 Classes/ 目录下,且无编译警告。

按此流程操作,你的 CocoaPods 库即可被全球开发者使用!

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

友情链接更多精彩内容