1、clone组件模板
首先我们来为项目创建一个关于题库的功能组件LYQuestionKit。在桌面创建一个名为Demo的文件夹,使用终端切换到该文件夹目录下。
//切换到Demo文件夹
cd /Users/zhouluyao/Desktop/Demo
//把https://github.com/CocoaPods/pod-template.git 远程的模板clone到本地
pod lib create LYQuestionKit
#此时命令行会有配置提示,按照如下选择
What platform do you want to use?? [ iOS / macOS ]
> iOS
What language do you want to use?? [ Swift / ObjC ]
> ObjC
Would you like to include a demo application with your library? [ Yes / No ]
> Yes
Which testing frameworks will you use? [ Specta / Kiwi / None ]
> None
Would you like to do view based testing? [ Yes / No ]
> No
What is your class prefix?
>LY
安装成功会有下面的提示,打开LYQuestionKit.xcworkspace 这个工程
Running pod install on your new library.
Analyzing dependencies
Downloading dependencies
Installing LYQuestionKit (0.1.0)
Generating Pods project
Integrating client project
[!] Please close any current Xcode sessions and use `LYQuestionKit.xcworkspace` for this project from now on.
Pod installation complete! There is 1 dependency from the Podfile and 1 total pod installed.
1.1、把想要分享的(.h,.m)文件放在ReplaceMe.m 同级目录,删除ReplaceMe.m文件
1.2、和之前开发项目一样,只是把一部分代码,现在放到Pods项目下Development Pods文件夹下的LYQuestionKit下面了
2、开发完后把组件代码与远程仓库关联
2.1、在公司的git服务器(或者GitHub上)创建一个LYQuestionKit仓库。
2.2、切换到LYQuestionKit目录下,从命令行创建一个新的仓库,与2.1中创建的git仓库进行关联,并推送到远程仓库
//切换到LYQuestionKit目录下
cd /Users/zhouluyao/Desktop/Demo/LYQuestionKit
//从命令行创建一个新的仓库
git init
git add .
git commit -m "first commit"
//与2.1中创建的git仓库进行关联,并推送到远程
git remote add origin https://github.com:99/zly/LYQuestionKit.git
git push -u origin master
3、打tag
为当前开发的组件代码设置tag、tag版本号和podspec的 s.version两者必须统一。
git tag '0.0.1' && git push --tags //每次提交更新都需要更新
在介绍步骤4、步骤5之前,先把pod install发生了什么,和发布一个podspec的流程梳理清楚
3.1、pod install发生了什么?
为了简化问题,我们假设项目中只依赖了一个库,pod 'AFNetworking', '4.0.0'
执行pod install之后会去本地的 ~/.cocoapods/repos/master/Specs 这个目录下查找AFNetworking所在的目录。
方法一:直接在 ~/.cocoapods/repos/master/Specs 本地搜索AFNetworking,不过这种方法苹果的索引搜索没有进行优化,搜索太慢了。
方法二(推荐):在浏览器输入 github.com/CocoaPods/S… ,然后搜索AFNetworking,如下图,会看到AFNetworking所在的索引目录,/specs/a/7/5/AFNetworking
我们很容易在本地~/.cocoapods/repos/master/Specs/a/7/5 目录下找到AFNetworking文件夹以及对应的版本号
在4.0.0版本号对应的文件夹下存在一个AFNetworking.podspec.json文件,这个文件里面最核心的信息就是下面这些
{
"name": "AFNetworking",
"version": "4.0.0",
"source": {
"git": "https://github.com/AFNetworking/AFNetworking.git",
"tag": "4.0.0",
"submodules": true
},
}
pod 'AFNetworking', '4.0.0',我们根据 '4.0.0'版本号找到了对应的文件夹,在AFNetworking.podspec.json文件的source里面可以看到pod库的代码仓库地址 https://github.com/AFNetworking/AFNetworking.git,然后就可以去下载源码了,说到这里大家应该也明白了 本地 ~/.cocoapods/repos/master/Specs目录就是远程github.com/CocoaPods/S… 仓库下clone的
3.2、发布podspec.json文件的流程
在步骤1 clone下来的pod 仓库模板文件下的LYQuestionKit.podspec 文件修改后(接下来的步骤4),执行pod trunk 命令(接下来的步骤5)发布到 github.com/CocoaPods/S… ,别人在本地执行pod update 就能把远程 github.com/CocoaPods/S… 最新的索引库同步到本地,就能发现你发布的pod库了
4、配置podspec
配置podspec包括 source_files、dependency、source、version等信息,若有子文件夹想要设置子模块,需要配置subspec,对编辑好的podspec进行验证。
//1、配置podspec格式如下
//pod名
s.name = 'LYQuestionKit'
//版本号
s.version = '0.0.1'
//要分享源代码的仓库地址,可以是自己的私有仓库
s.source = { :git => 'https://github.com:99/zly/LYQuestionKit.git', :tag => s.version.to_s
//源文件地址
s.source_files = 'LYQuestionKit/Classes/**/*'
//设置第三方库的依赖
s.dependency 'FMDB', '~>2.7.2'
//子模块
s.subspec 'LYConfig' do |s|
s.source_files = 'LYQuestionKit/Classes/LYConfig/*'
s.public_header_files = 'LYQuestionKit/Classes/LYConfig/*.h'
end
//2、找到Example下面的podfile文件进行pod install
//3、验证podspec是否正确,pod lib lint 不会访问网络
pod lib lint --allow-warnings #本地验证,如果验证成功会提示LYQuestionKit passed validation.
5、发布到 Cocoapods
//1、验证 `podspec` 文件信息是否可以通过验证 pod spec lint checks the external repo and associated tag.
pod spec lint
//2、提交之前要先通过 trunk 注册生成一条会话,点击邮箱验证,不然第三步过不了
// pod trunk register 邮箱 用户名 描述
pod trunk register 437001178@qq.com zhouluyao --description=LYQuestionKit题库
//3、将 podspec 提交给 CocoaPods 了,这个文件将是别人搜索你的组件的索引。
pod trunk push LYQuestionKit.podspec --allow-warnings
组件代码发布新版本则需要打新的 tag,重新编辑 podspec 文件,然后再次提交给 CocoaPods。
常见问题解决方案
问题1、执行 pod search LYQuestionKit 找不到,报以下错误
[!] Unable to find a pod with name, author, summary, or description matching LYQuestionKit
解决方案
//如果搜索不到,删除本地的搜索文件
rm ~/Library/Caches/CocoaPods/search_index.json
//重新 search 产生新的搜索文件
pod search LYQuestionKit
参考资料: iOS 组件化