一、简介
公有库:开源自己封装的库供别人使用,且往cocoaPods的官方Repo仓库(即CocoaPods Master Repo)中新增自己库的索引,该库索引是以*.podspec.json文件形式存在。
私有库:私有库不在cocoaPods的官方Repo仓库中,而是在自建的spec repo中,库索引是以*.podspec文件形式存在,自建的spec repo放在路径~/.cocoapods/repos下,与master同级,其链接的远程仓库可以是公开的,也可以是私有的。
Spec Repo表示Repo仓库,是集合所有公开库的.podspec.json文件(索引文件)的git仓库。
当使用Cocoapods时它会被clone到本地的~/.cocoapods/repos目录下,进入目录后可以看到master文件夹,它就是官方的Spec Repo本地仓库。
因此创建CocoaPods私有库就需要有类似master的私有Spec Repo,让它关联远程git仓库。
二、搭建私有库和公有库
私有库和公有库前面搭建步骤是相同的,只在发布时操作不同。
1.github上新建空白项目YJPodTest。仓库网址为https://github.com/Manchitor/YJPodTest.git,该仓库链接我们的自定义库YJPodTest。

2.创建Pods工程。选择你的代码库目录,执行命令pod lib create YJPodTest,依次回答终端问题。

没关系,GitHub 会抽风,我们重试一下


没关系,我们去YJPodTest/ Example项目里手动执行pod install

3.编辑库文件,新建一个工具类 YJPodTestTool。



并将这两个库文件放入YJPodTest/Classes中,删掉ReplaceMe.m文件。

Example是创建Pods工程时生成的一个工程,用于验证添加的库文件是否能正常使用。Example目录下Podfile文件中自动设置了YJPodTest依赖,如下:

所以我们删除项目中我们新建的YJPodTestTool.h 和YJPodTestTool.m 其本意是模拟pod导入来自YJPodTest/Classes中的文件。
终端进入到Example下执行pod install 安装依赖

这个时候我们发现刚才我们YJPodTest/Classes下的库文件已经通过pod导入进来了

在Example 项目中就可以导入头文件并使用了

4.提交pods库到github上
由于pod lib创建YJPodTest工程时默认已经准备了LICENSE、README.md、.gitignore三个文件,若在github上创建新建空白项目pod_test时勾选了这三个文件,则git pull时会产生冲突。解决方式:将YJPodTest工程目录下与github项目冲突的文件删除。
此外,YJPodTest目录下默认准备了.git仓库,会影响与github仓库的同步,删除掉。
cd到YJPodTest目录下
初始化git 并提交

推送到远程仓库 命令为:git push origin master:main

此时你的github上就已经更新了

5.配置Pods工程的.podspec文件。主要是s.version、s.summary、s.source的编辑。


s.source_files = ‘YJPodTest/Classes//’,这里表示匹配所有子目录,表示匹配所有文件。
6.打标签,推送到远程。重点:tag打的版本号必须和podspec中的s.version值相同

7.验证.podspec文件的格式是否正确,必须打完tag才能验证,且必须验证,否则后面执行pod repo push或pod trunk push一定失败。
执行命令


8.发布到私有库到Repo仓库
github上新建空白项目PodSpecTest,网址https://github.com/Manchitor/PodSpecTest.git,作为Repo仓库

在新建的文件目录下提交一个README.md 初始化git仓库,这里我创建的YJPodSpecTest 跟YJPodTest 同级

到YJPodSpecTest目录下初始化git仓库命令:
echo "# PodSpecTest" >> README.md
git init
git add README.md
git commit -m "first commit"
git branch -M main
git remote add origin https://github.com/Manchitor/PodSpecTest.git
git push -u origin main
回到本地生成私有的Spec Repo工作目录上层,执行
pod repo add YJPodSpecTest https://github.com/Manchitor/PodSpecTest.git

那么在路径~/.cocoapods/repos下就会有YJPodTest本地仓库。

把YJPodTest.podspec提交到自建的Spec Repo仓库:

执行命令:将 YJPodTest.podspec 推到自己创建的Spec Repo (YJPodSpecTest)进行管理
pod repo push YJPodSpecTest YJPodTest.podspec

报错如下:没关系 加上 --allow-warnings --use-libraries

执行命令:pod repo push YJPodSpecTest YJPodTest.podspec --allow-warnings --use-libraries

这样私有库就制作好了。我们用pod search 'YJPodTest'试一下看能不能搜索到。

附带一张GitHub Spec Repo 仓库截图

9.发布到公有库
pod trunk push YJPodTest.podspec --allow-warnings --use-libraries
若显示"pod_test successfully published"就表示成功发布到官方Spec Repo仓库了。
若报错没有注册CocoaPods账号,则注册一个,命令(github_email填邮箱地址,'user_name’填作者名称):
pod trunk register github_email 'user_name' —verbose
然后到邮箱中收取邮件来点击验证注册,之后再执行一次pod trunk push pod_test.podspec --allow-warnings即可。(提醒:换台电脑,修改源码,提交到仓库时一样需要注册) 至此,公有库pod_test完成。 提交之后cocoaPods官网更新会延迟,耐心等待5-10分钟,然后将其更新到本地master spec repo
[图片上传失败...(image-ec5be3-1679368636189)]
网友评论