一、理解spec repository和code repository
spec repository和code repository是什么?一直被这两个东西给绊着。
code repository:代码仓库。我们把包代码上传到这个仓库里。
spec repository:配置仓库。所有的配置按照包名、版本号分门别类的存放在这个仓库。注意: 这个仓库只用来存放spec文件,不存放代码。(tips:创建远程仓库时:需创建README.md,否则仓库为空时,上传会报错)。
之前,笔者一直卡在这地方,将代码和. spec文件放在同一个git仓库下,所有导致pod spec lin或者intsall的时候总报错。
二、步骤概要
1.在gitlab上创建属于自己的远程索引库,用来存放.podspec文件和代码

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

2.2 $ pod repo add 本地仓库名(本地pod repos私有库的名称) sourceURL (上一步建好的远程spec仓库)
eg:我给自己的私有库取名为HZTagKitSpec

2.3 关联后 终端输入 pod repo,查看本地已存在的索引库

3.快速创建模板工程
3.1 cd到目标目录下,$ pod lib create 文件名
eg:我是直接在桌面创建的
$ pod lib create HZTagKit

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

备注:我直接删掉了原有的Assets和Classes文件夹,替换为自己的组件
(特别需要注意的是:有要用到图片文件的地方,必须写好bundle路径)

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

4. 编辑工程里的.podspec文件

我的设置参考下图

$ pod lib lint 验证 ( $ pod lib lint --allow-warnings 忽略警告)
表明验证通过

5 将测试工程代码提交到第三步创建好的远程代码管理仓库
$ git status
$ git add .
$ git commit -m '组件化配置'
$ git remote add origin 远程代码仓库地址 (将本地库与远程代码仓库进行关联)
$ git push origin master (提交到远程仓库)
$ git tag '0.1.0' (要与HZTagKit.podspec文件中的tag值保持一致)
$ git push --tags(将tag提交到远程)
$ pod spec lint --allow-warnings(验证远程是否正确)
验证通过

6. 将podspec文件提交到本地的私有索引库
$ pod repo push HZTagKitSpec HZTagKit.podspec --allow-warnings

提交成功,此时远程私有库添加到此结束
7.主工程利用Pods使用我们自己的框架
$ pod install

8. 更新维护组件
Tips:添加图片
把图片资源拖入Assets文件夹,需要修改.podspec文件,打开资源的加载路径,然后执行pod install。然后会发现多了一个Resource的资源文件夹。

.podspec 文件中写明图片路径

最后pod之后会生成一个Resources文件夹

我们发现图片实际是在HZBottomSelectView组件下HZBottomSelectView.bundle 里面,因此需要改变图片的加载方式.

说明:必须指明图片的全名和图片所在bundle的包名.
加载图片

注意:必须写全图片名称"hz_bottom_select_tag.png",否则图片不会显示
参考:https://www.jianshu.com/p/1f56c3d78b52
网友评论