一、创建自己的git账号,并将自己的电脑与指定github账户关联。
二、创建本地仓库
1.创建本地库
打开终端,进入要创建本地库的文件夹,创建项目
cd 创建本地库的文件夹路径
pod lib create 项目名称,根据自己需求定义就好
输入完成后,终端会显示下面几个问题,根据实际情况选择即可

创建完成后会自动打开创建的项目,暂时不用管。找到刚创建的项目并打开,目录如下。如果没有显示那几个灰色文件(.git),说明你电脑没有打开隐藏文件查看,用时按住command健、Shift健和.健即可。此时本地仓库创建完成。

三、将源码导入项目
1.创建自己要使用的代码,打开一下项目在里面创建自己要使用的文件,然后写上自己的要用于三方库的代码。
2.将代码文件放到Classes文件下

3.通过Xcode打开WSTestCocoa.podspec文件(WSTestCocoa为我创建的项目名称),配置本地仓库支持的CocoaPods文件,如下:

四、创建远端仓库
1.创建库
为了避免不必要麻烦,远端仓库名称和本地仓库最好一致

2.复制远端仓库的地址

3.继续配置.podspec文件
首先检查下WSTestCocoa.podspec里面s.source的路径和远端仓库路径是否一致,不一致改成远端仓库路径,让后在终端cd 到Example文件目录下执行pod update --no-repo-update命令。

4.此时已经添加完成
打开Example里面的测试demo,在demo里面就可以使用上面引入的三方库了。

5.验证一下.podspec文件
回到本地仓库根目录下面,执行pod lib lint命令。
如果警告会执行失败,此时可以修改警告内容,也可以直接执行pod lib lint --allow-warnings命令,忽略警告。

看到显示WSTestCocoa passed validation就表示配置成功了。
6.把依赖库上传到git并打上标签tag
回到本地仓库根目录下面,添加远端仓库地址,执行命令
git remote add origin 远端仓库地址
把所有修改过的文件添加到缓存区
git add .
提交变动记录
git commit -m “代码提交”

遵循先拉后传原则,先拉取远程代码。
git pull
然后推送代码到远端仓库
git push
出现问题,不用管,强制覆盖。
git push --force 远端仓库地址

然后给依赖库打tag标签,注意,这个标签的版本号一定要和WSTestCocoa.podspec中的s.version一致
git tag 0.1.0
将tag标签推送到远端仓库,版本号同样和WSTestCocoa.podspec文件中的s.version一致
git push origin 0.1.0

到这里,本地仓库和远端仓库都创建并且验证完成了,想要支持CocoaPods安装,还必须要一个管理仓库,也叫索引库。索引库和仓库一样也是需要两个,一个本地的一个远端的。
五、创建远端索引库
1.创建远端索引库
创建远端索引库的方式和创建远端仓库的方式一样,如下:

2.复制远端仓库的地址

六、创建本地索引库
1.创建本地索引库,回到要创建本地库的文件夹执行下面命令把远端索引库和本地索引库关联起来,强烈建议本地索引库和远端索引库使用一样名称,避免不必要的bug出现。
pod repo add 本地索引库名字 远端索引库的地址

2.验证是否创建成功
可以直接在终端输入命令查看创建结果,终端命令如下:
pod repo

3.绑定WSTestCocoa.podspec文件到远端索引库
需要把WSTestCocoa.podspec文件推送到远端索引库进行绑定操作,终端需要回到本地仓库所在的目录下,再执行命令,终端命令如下:
pod repo push 索引库名字 本地仓库名字.podspec
出现下图表示成功:

七、发布到Cocoapods官方库中
回到本地依赖库根目录,先查看是否注册过pod账号。如果没有注册,注册一下,注册命令
pod trunk register 邮箱地址 '名称'

执行成功后,回发送一份邮件到邮箱,此时登陆邮箱点击邮件中链接即可注册成功。
然后把WSTestCocoa.podspec提交到官方Spec Repo仓库,命令如下:
pod trunk push WSTestCocoa.podspec --allow-warnings
出现下图,则表示成功了

此时执行pod search WSTestCocoa命令就可以搜索到了,如果搜索不到,等待一会,可能会有延迟
八、使用
跟使用其他三方库一样,vim Podfile,然后将pod 'WSTestCocoa', '~> 0.1.0' 添加进去 pod install,如果报错,执行pod repo update命令更新下。
此时就可以在项目中使用这个三方库了。
九、更新
更新就是重新走一遍上传新代码、打标签、上传标签、绑定远端索引库、发布,其中谨记修改version。
git pull
git add .
git commit -m "0.2.0"
git push
git push --force https://github.com/sun0618/WSTestCocoa.git
git tag 0.2.0
git push origin 0.2.0
pod repo push WSTestCocoaSpec WSTestCocoa.podspec
pod trunk push WSTestCocoa.podspec --allow-warnings
十、gitee
如果使用gitee仓库进行管理,需要进行下列修改
1、创建github仓库改成创建gitee仓库(里面相关的github仓库地址全部替换成gitee仓库地址)
2、使用的时候将gitee仓库地址添加到Podfile文件里面

3、pod install如果报下面的错误,找到/Users/yang.sun/.cocoapods/repos/gitee-winsafe_app-wsregister/WSRegister文件夹,删除里面的Assets和Classes文件。如果不行先pod repo update在试试。(有网友说在最初创建的时候,新建一个Sources文件夹,将代码放在这里面就没有问题了,未测试)。

十一、添加图片
如果库要用到图片,对图片的使用方法跟正常情况下不同。
1、将图片命好名后添加到Assets文件夹里面(跟Classes文件夹同一目录层级)。

2、修改WSRegister.podspec文件里面s.resource_bundles如下。

3、在使用的地方用NSBundle获取。

4、尽管在当前项目里面图片会跟class文件显示在同一目录层级下面,但是集成到其他项目后,图片会自动归类到Resources文件夹下面。
网友评论