johnhao
johnhao
发布于 2020-01-14 / 12 阅读
0

Cocoapods实现私有库

在 iOS 开发中,使用 CocoaPods 实现私有库(Private Pod)是一种常见的模块化和代码复用方式。以下是详细步骤,教你如何创建并使用自己的私有 CocoaPods 库。


一、准备工作

1. 安装 CocoaPods(如未安装)

sudo gem install cocoapods

2. 创建私有 Git 仓库(用于存放 spec 和源码)

  • 在 GitHub / GitLab / Bitbucket / 内部 Git 服务器上创建两个私有仓库:

    • Specs 仓库:用于存放 .podspec 文件(例如 MyPrivateSpecs.git

    • 源码仓库:用于存放你的私有库源代码(例如 MyPrivateLib.git

注意:这两个仓库可以合并为一个(推荐分开,更清晰)。


二、创建私有 Pod 源码库

1. 创建 Pod 项目结构

pod lib create MyPrivateLib

按提示选择语言(Swift/ObjC)、是否包含 Demo 等。

这会生成如下结构:

MyPrivateLib/
├── Example/
├── MyPrivateLib/
│   └── Classes/
├── MyPrivateLib.podspec
└── ...

2. 编写你的代码

将你的功能代码放入 MyPrivateLib/Classes/ 目录中。

3. 配置 .podspec 文件

编辑 MyPrivateLib.podspec,确保以下字段正确:

Pod::Spec.new do |s|
  s.name             = 'MyPrivateLib'
  s.version          = '0.1.0'
  s.summary          = 'A short description of MyPrivateLib.'
  s.description      = 'Longer description...'
  s.homepage         = 'https://github.com/yourname/MyPrivateLib' # 源码仓库地址
  s.license          = { :type => 'MIT', :file => 'LICENSE' }
  s.author           = { 'Your Name' => 'you@example.com' }
  s.source           = { :git => 'https://github.com/yourname/MyPrivateLib.git', :tag => s.version.to_s }
  s.ios.deployment_target = '12.0'
  s.source_files = 'MyPrivateLib/Classes/**/*'
  s.swift_version = '5.0' # 如果是 Swift
end

⚠️ 注意:s.source 必须指向你的源码仓库,并且 tag 要与版本一致。

4. 提交代码并打 Tag

git init
git add .
git commit -m "Initial commit"
git remote add origin https://github.com/yourname/MyPrivateLib.git
git push origin main

# 打 tag(必须与 podspec 中的 version 一致)
git tag 0.1.0
git push --tags

三、创建私有 Specs 仓库

1. 初始化私有 Specs 仓库

在你的 Git 平台新建一个空仓库,比如 MyPrivateSpecs.git

2. 将本地 Podspec 推送到私有 Specs 仓库

# 克隆你的私有 Specs 仓库(如果尚未存在)
pod repo add MyPrivateSpecs https://github.com/yourname/MyPrivateSpecs.git

# 验证 podspec 是否合法
pod lib lint MyPrivateLib.podspec --private --allow-warnings

# 将 podspec 复制到本地 specs 仓库,并提交推送
pod repo push MyPrivateSpecs MyPrivateLib.podspec --private --allow-warnings

成功后,你的 MyPrivateSpecs 仓库中会多出目录:MyPrivateLib/0.1.0/MyPrivateLib.podspec


四、在项目中使用私有 Pod

在你的主项目 Podfile 中添加:

source 'https://github.com/CocoaPods/Specs.git'        # 公共源(可选)
source 'https://github.com/yourname/MyPrivateSpecs.git' # 你的私有源

platform :ios, '12.0'

target 'YourApp' do
  pod 'MyPrivateLib', '~> 0.1.0'
end

然后执行:

pod install

五、更新私有 Pod

  1. 修改代码 → 更新 .podspec 中的 version(如 0.2.0

  2. 提交代码并打新 tag

  3. 执行 pod repo push MyPrivateSpecs MyPrivateLib.podspec --private --allow-warnings

  4. 在主项目中运行 pod update MyPrivateLib


常见问题

Q: 如何跳过验证?

A: 使用 --skip-import-validation--allow-warnings,但不建议长期跳过。

Q: 私有库依赖其他私有库?

A: 在 .podspec 中添加:

s.dependency 'AnotherPrivateLib', '~> 1.0'

并在 Podfile 中同时指定多个 source。

Q: 不想建单独的 Specs 仓库?

A: 可以直接使用源码仓库的根目录放 .podspec,然后在 Podfile 中这样引用:

pod 'MyPrivateLib', :git => 'https://github.com/yourname/MyPrivateLib.git', :tag => '0.1.0'

但这样无法使用语义化版本管理(不能 ~> 0.1.0 自动升级)。


总结

步骤

说明

1

创建源码仓库 + Specs 仓库

2

pod lib create 生成模板

3

编写代码 & 配置 .podspec

4

提交代码 + 打 tag

5

pod repo push 发布到私有 Specs

6

在项目 Podfile 中指定私有源并引入


如果你使用的是 公司内网 Git 服务器,只需把上述所有 https://github.com/... 替换为你的内网地址即可。