笔者属于比较笨的类型,前后查阅了很多文章,尝试了N+2次才慢慢摸索出如何使用cocoapods创建私有库。
一、理解spec repository和code repository
spec repository和code repository是什么?一直被这两个东西给绊着。
code repository:代码仓库。我们把包代码上传到这个仓库里。
spec repository:配置仓库。所有的配置按照包名、版本号分门别类的存放在这个仓库。注意: 这个仓库只用来存放spec文件,不存放代码。
以AFNetWorking为例,如果只创建AFNetWorking的code repository,而不将code repository中的.podspec文件上传到spec repository的话,那么工程引入AFN的时候压根用不了。
之前,笔者一直卡在这地方,将代码和. spec文件放在同一个git仓库下,所有导致pod spec lin或者intsall的时候总报错。
二、步骤概要
1.创建远程代码库,用来保存.podspec、LICENSE、README.md以及代码文件和一些资源文件,并与本地代码关联。
2.创建私有cocoapods 仓库,用来存放.podspec文件,来管理自己的私库。
三、开始动手
1、在码云上创建远程私有索引库,用来存放.podspec文件

2、创建本地的私有索引库文件夹,并与远程私有索引库进行关联
2.1 $ pod repo(查看本地已存在的索引库)

2.2 添加本地私有索引库并与远程私有库
$ pod repo add 本地文件名 sourceURL (上一步建好的远程仓库)
例如我本地建立了一个名为GMLFMBaseSpec的文件用来关联https://gitee.com/candygu2011/GMLFMBaseSpec.git
2.3 关联后 终端输入 pod repo,查看本地已存在的索引库


3、在码云创建一个用来存放项目基础组件的仓库GMLFMBase

4、创建模板测试工程
4.1 cd到目标目录下 $ pod lib create GMLFMBase

4.2 填写以上信息后Xcode会自动打开测试工程,在测试模板工程文件夹下,用自己建好的组件替换掉原来的ReplaceMe.m文件,结果如下:

4.3 回到Example路径下,重新执行pod install操作
此时我们可以看到Pods > Development Pods > GMLFMBase > (.h .m)多了我们添加的类目文件

5、将测试工程代码提交到第三步创建好的远程代码管理仓库
$ git status
$ git add .
$ git commit -m'基础组件测试工程'
编辑GMLFMBase 模版工程的GMLFMBase.podspec文件

$ pod lib lint 验证 ( $ pod lib lint --allow-warnings 忽略警告)
$ git status
$ git add .
$ git commit -m '编辑spec文件'
$ git remote add origin [https://gitee.com/candygu2011/GMLFMBase.git] (将本地库与远程代码仓库进行关联)
$ git push origin master (提交到远程仓库)
$ git tag '0.1.0' (要与GMLFMBase.podspec文件中的tag值保持一致)
$ git push --tags(将tag提交到远程)
$ pod spec lint --allow-warnings(验证远程是否正确,注意:Username与Password一定要填写正确)
6、将podspec文件提交到本地的私有索引库
pod repo push GMLFMBaseSpec GMLFMBase.podspec --allow-warnings

表示提交成功,此时远程私有库到此就已结束。
7、主工程使用Pods引用私有框架
7.1
cd 到主工程目录
$ pod init
用Xcode打开编辑Podfile文件

$ pod install
7.2 更新维护podspec。
当我们的代码变动时,首先需要更新code Repository中的.podspec 文件的版本,然后push到 Spec Repository。
8、参考资料
cocoapods
如何创建cocoapods私有仓库
CocoaPods远程私有库从0到1
An unexpected version directory Assets
was encountered for Private Pod
以及一些网络视频
网友评论