制作 CocoaPods 依赖库
2018-11-26 本文已影响13人
浮浅丶Superficial
1、创建自己的 github 仓库
CocoaPods 是托管在 github 上的,所有的 Pods 也都是托管在 github 上,因此我们首先需要创建一个属于自己的 github 仓库,并 clone 仓库到本地
![](https://img.haomeiwen.com/i1983045/8ae07c9bfb9d8463.png)
![](https://img.haomeiwen.com/i1983045/a83bab617d319934.png)
2、创建 .podspec 文件
cd 到本地仓库下(README.md 目录),执行
pod spec create MHKeyValueStore
目录下会生成 MHKeyValueStore. podspec 的文件
3、添加上传到 Pods 所需要的文件,并创建 Demo 工程最终目录文件如下:
![](https://img.haomeiwen.com/i1983045/558c0a109bd2d92b.png)
4、一顿骚操作后上传修改到GitHub,并添加 tag
![](https://img.haomeiwen.com/i1983045/47994f2a6e3143b6.png)
5、准备工作
5.1 注册上传 CocoaPods 的账号
pod trunk register yan_yan.vip@foxmail.com 'MrAiden'
邮箱为 github 邮箱,用户名最好为 github 用户名,执行完,点击邮箱连接,完成注册。
查看信息, 执行
pod trunk me
![](https://img.haomeiwen.com/i1983045/03a28fe441157156.png)
5.2 配置 .podspec 文件
打开 MHKeyValueStore. podspec 文件,大致如下:
Pod::Spec.new do |s|
s.name = "MHKeyValueStore" // 名称
s.version = "0.0.1" // 版本号,与 tag 一致
s.summary = "A short description of MHKeyValueStore." // 摘要
s.description = "A description of MHKeyValueStore." // 描述
s.homepage = "https://github.com/MrAiden/MHKeyValueStore" // 首页
s.license = { :type => "MIT", :file => "LICENSE" } // 许可证
s.author = { "Mortar" => "yan_yan.vip@foxmail.com" } // 作者
s.platform = :ios, "8.0" // 平台信息
s.source = { :git => "https://github.com/MrAiden/MHKeyValueStoreDemo.git", :tag => "#{s.version}" } // git源文件
s.source_files = "MHKeyValueStore/**/*.{h,m}" // 需要上传的文件
/**
"*" 表示匹配所有文件
"*.{h,m}" 表示匹配所有以.h和.m结尾的文件
"**" 表示匹配所有子目录
*/
s.requires_arc = true // ARC
s.dependency 'FMDB', '~> 2.7.5' // 关联的第三方
end
5.3 验证 .podspec 文件
pod spec lint MHKeyValueStore.podspec
有错改错。如有警告:
[!] The spec did not pass validation, due to 2 warnings (but you can use `--allow-warnings` to ignore them).
可忽略警告验证:
pod lib lint --allow-warnings
输出如下结果,则通过验证:
MHKeyValueStore passed validation.
5.4 上传
pod trunk push
或
pod trunk push MHKeyValueStore.podspec
或忽略警告上传
pod trunk push --allow-warnings
输出如下结果,则上传成功
--------------------------------------------------------------------------------
🎉 Congrats
🚀 MHKeyValueStore (1.0.0) successfully published
📅 November 25th, 20:41
🌎 https://cocoapods.org/pods/MHKeyValueStore
👍 Tell your friends!
--------------------------------------------------------------------------------
6、可能错误信息
6.1 [!] The master
repo is not a git repo.
// 终端输入以下命令
rm-rf~/.cocoapods
pod setup
6.2 [!] podRoutes1 did not pass validation, due to 1 warning (but you can use --allow-warnings
to ignore it)
// 忽略警告
pod lib lint --allow-warnings
6.3 [!] The podspec does not validate.
原因很多,通过追加--verbose参数查看具体原因
// 在命令行后面添加 --verbose
pod trunk push MHKeyValueStore.podspec --verbose
// 如果处在错误,则会详细注明,例如
warning: Could not find remote branch 0.0.1 to clone.
fatal: Remote branch 0.0.1 not found in upstream origin
eg:
// ERROR | [iOS] file patterns: The `source_files` pattern did not match any file.
确认source_files的匹配是否成功,可以用*.*模糊匹配
6.4 remote: Permission to HH-Medic/HHJumpRoutes.git denied to 515783034.fatal: unable to access 'https://github.com/MrAiden/MHKeyValueStoreDemo.git/': The requested URL returned error: 403
//没有访问权限,修改下git/config
url = https://github.com/MrAiden/MHKeyValueStoreDemo.git
url = https://MrAiden@github.com/MrAiden/MHKeyValueStoreDemo.gitt // 添加账户即可,第一次会让你输入账号的密码
6.5 Unable to find a pod with name matching (or pod search时提示查找不到)
// 更新一下本地缓存的框架
pod repo remove master
6.6 Unable to find a specification for 'MHKeyValueStore'
// 可能原因:podspec文件配置错误, 注意查看以下问题等
1. 单引号的问题;
2. 源文件路径;
6.7 Could not find remote branch 1.0.1 to clone.
// 找不到对应的tag, tag不一致
1. 是否push 了tag;
2. podspec中是否修改了对应的tag;
git push --tags
s.version = "1.1.5"