描述:我们开发iOS 项目时,几乎每个工程都会集成用cocoapods 管理第三方库,于是就有这样的需求–我分装了一个独立的SDK模块,想让其他人通过pod search 到,并能使用这个开源的SDK。这里就记录一下过程。
例如:pod ‘JSONKit’ 我们在pod 中下载到JSONKit,是因为JSONKit 制作了管理JSONKit的 文件podspec,它是描述库的基本信息。在pod 服务器上注册过了,并且代码传到pod 服务器上了,我们pod 集成到项目中时,会从远端下载JSONKit到本地git。pod 工程管理引用的库。
- 在Github 上建立一个SDK 开源repository:MYHexTool。
⚠️:记得Add a license 不然很麻烦。
2. 使用SourceTree checkout repository:MYHexTool到本地
地址 https://github.com/muyushifang07/MYHexTool.git
- 将代码放到本地的MYHexTool文件夹中
- SourceTree 提交到远程。需要你的github.com 上的用户名、密码
Github 查看。已经存在了上传的代码。
- 因为podspec文件中获取Git版本控制的项目还需要tag号,所以在编辑podspec文件之前我们要打上一个tag:
$ cd 文件目录
$ git tag -m "first release" "0.1.0"
$ git push --tags #推送tag到远端仓库
需要github用户名、密码
可以看到tag下面一栏有 0.1.0版本(也可以点击release下面有tag)
6. 为创建podspec文件
pod spec create MYHexTool
- 编辑podspec内容,大概浏览一下,知道什么意思,修改为下面的:根据自己的实际情况修改
Pod::Spec.new do |s|
s.name = "MYHexTool"
s.version = "0.1.0"
s.summary = "制作自己的pod库 MYHexTool."
s.homepage = 'https://github.com/muyushifang07/MYHexTool.git'
s.license = "MIT"
# s.license = { :type => "MIT", :file => "FILE_LICENSE" }
s.author = { 'muyushifang07 ' => 'https://blog.youkuaiyun.com/shifang07/article/details/90517002' }
s.platform = :ios
s.source = { :git => "https://github.com/muyushifang07/MYHexTool.git", :tag => "0.1.0" }
s.source_files = 'VideoPlayerLib.{h,m}'
end
s.source_files = ‘MYHexTool/VideoPlayerLib.{h,m}’ 这里要注意,改成下面的路径
s.source_files = ‘VideoPlayerLib.{h,m}’
- 检验pod spec是否可用:pod spec lint
[可以添加–verbose来查看具体信息,这里信息比较多,不建议使用,可以没事看看]
lzz-Mac-mini:MYHexTool suning$ pod spec lint
-> MYHexTool (0.1.0)
- WARN | [iOS] license: Unable to find a license file
- NOTE | xcodebuild: note: Using new build system
- NOTE | [iOS] xcodebuild: note: Planning build
- NOTE | [iOS] xcodebuild: note: Constructing build description
- NOTE | [iOS] xcodebuild: warning: Skipping code signing because the target does not have an Info.plist file. (in target 'App')
Analyzed 1 podspec.
[!] The spec did not pass validation, due to 1 warning (but you can use `--allow-warnings` to ignore it).
[!] The validator for Swift projects uses Swift 3.0 by default, if you are using a different version of swift you can use a `.swift-version` file to set the version for your Pod. For example to use Swift 2.3, run:
`echo "2.3" > .swift-version`.
lzz-Mac-mini:MYHexTool suning$ pod spec lint --allow-warnings
-> MYHexTool (0.1.0)
- WARN | [iOS] license: Unable to find a license file
- NOTE | xcodebuild: note: Using new build system
- NOTE | [iOS] xcodebuild: note: Planning build
- NOTE | [iOS] xcodebuild: note: Constructing build description
- NOTE | [iOS] xcodebuild: warning: Skipping code signing because the target does not have an Info.plist file. (in target 'App')
Analyzed 1 podspec.
- 出现的几个错误
执行 pod lib lint 时,报错:
- ERROR | [iOS] unknown: Encountered an unknown error (Could not find a
ios
simulator (valid values: com.apple.coresimulator.simruntime.ios-10-3, com.apple.coresimulator.simruntime.ios-12-1, com.apple.coresimulator.simruntime.ios-8-1, com.apple.coresimulator.simruntime.tvos-12-1, com.apple.coresimulator.simruntime.watchos-5-1). Ensure that Xcode -> Window -> Devices has at least oneios
simulator listed or otherwise add one.
解决办法: 升级CocoaPods sudo gem install cocoapods
再次执行 pod lib lint, 验证通过
- WARN | [iOS] license: Unable to find a license file 原因:没有找到License文件,根据规则也可以直接把文本卸载podspec文件里
解决方案:文件里面加一句即可:
s.license = “Copyright © 2015年 Lisa. All rights reserved.”
或者看这个Unable to find a license file warning
s.license = { :type => “MIT”, :file => “LICENSE” }
- 最终编译成功:passed
- 发布cocoapod,使用cocoapod的trunk服务进行发布spec,在这之前需要注册自己的电脑才能使用该功能,这很简单,只要你指明你的邮箱地址(spec文件中的)和电脑名称即可。
$ pod trunk register 1522344335@qq.com 'lzz-Mac-mini'
然后邮件验证
- 如果注册成功会有log提示,然后就可以发布自己的podspec了
$ pod trunk push MYHexTool.podspec
-
push podspec的过程比较慢,多等待一会,如果成功会出现下面的log
我这一步:发生错误。我的cocoapods -1.2.0,可能是这个原因。在最新的1.7.4成功了。
-
验证用 pod search MYHexTool 搜索结果??????
感谢:https://www.jianshu.com/p/b3732d8b8837
http://www.pianshen.com/article/6172249680/。(这篇原理介绍的非常好)
框架 trunk push 到 CocoaPods 成功后 pod search 却搜不到的解决方法