上篇文章[Cocoapods发布框架]:http://www.jianshu.com/p/e7c2047e3de1 的文章中,主要介绍了整个流程第一次配置的大致过程, 当然文章看上去是一番丰顺的. 但是, 事实并不是这样. 上篇文章主要讲的是第一次配置的步骤,所以文章篇幅过长, 现把抓哟步骤整理一下,顺便也把遇到的问题单独列了出来.
其实让自己的框架支持cocoapods,只需要几步
1.注册Trunk
2.创建/编辑.podspec
3.将自己的项目打成tag
4.验证
5.发布
用到的主要命令
1.注册Trunk
pod trunk register allisonwangjj@163.com 'Allisonwangjj' --description='第一次发布框架'
[!] Please verify the session by clicking the link in the verification email that has been sent to allisonwangjj@163.com
wuxianlvzhoudeMacBook-Pro:~ Allison$ pod trunk me
此时会收到一封邮件,点击邮件中的链接后验证一下即可.
2.创建/编辑.podspec
wuxianlvzhoudeMacBook-Pro:~ Allison$ cd /Users/wuxianlvzhou/FFAdditions/Additions
//2.1 创建FFAdditions框架的Podspec描述文件
wuxianlvzhoudeMacBook-Pro:Additions Allison$ pod spec create https://github.com/AllisonWangJiaoJiao/FFAdditions
2.2 编辑.podspec
创建好后打开,删除注释, 前面有#的为注释,整理好你所需要的文件
3.将自己的项目打成tag
执行命令
//为git打tag
git tag "0.0.1"
//将tag推送到远程仓库
git push --tags
4.验证
wuxianlvzhoudeMacBook-Pro:Additions Allison$ pod spec lint
验证成功后会出现如下信息:
FFAdditions.podspec passed validation.
注意
此时可能并不会一帆风顺,可能会出现如下错误
- ERROR
-> FFAdditions (0.0.1)
- ERROR | attributes: Missing required attribute `summary`.
- ERROR | [iOS] unknown: Encountered an unknown error (The `FFAdditions` pod failed to validate due to 1 error.
[!] 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`:
- ERROR | attributes: Missing required attribute `summary`.
) during validation.
Analyzed 1 podspec.
[!] The spec did not pass validation, due to 2 errors.
[!] 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`.
原因:没有填写summary信息,需要添加描述信息,如: s.summary = "第一次发布框架"
5.发布
wuxianlvzhoudeMacBook-Pro:Additions Allison$ pod trunk push
如果顺利的话,发布成功会出现如下界面:
Validating podspec
-> FFAdditions (0.0.3)
Updating spec repo `master`
--------------------------------------------------------------------------------
🎉 Congrats
🚀 FFAdditions (0.0.3) successfully published
📅 April 26th, 07:55
🌎 https://cocoapods.org/pods/FFAdditions
👍 Tell your friends!
--------------------------------------------------------------------------------
发布的时候常见的错误:
- ERROR
[!] Found podspec `FFAdditions.podspec`
Updating spec repo `master`
Validating podspec
-> FFAdditions (0.0.1)
[!] Unable to accept duplicate entry for: FFAdditions (0.0.1)
原因:意思是:不能接受同一版本的提交
解决方案:
5.1、执行如下命令
wuxianlvzhoudeMacBook-Pro:Additions Allison$ git tag 0.0.2
wuxianlvzhoudeMacBook-Pro:Additions Allison$ git push --tags
wuxianlvzhoudeMacBook-Pro:Additions Allison$ git push origin master
5.22、修改FFAdditions.podspec文件中的内容
Pod::Spec.new do |s|
s.name = "FFAdditions"
s.version = "0.0.2"
s.summary = "第一次发布框架"
s.homepage = "https://github.com/AllisonWangJiaoJiao/FFAdditions"
s.license = "MIT"
s.author = { "AllisonWangJiaoJiao" => "email@address.com" }
s.platform = :ios, "8.0"
s.source = { :git => "https://github.com/AllisonWangJiaoJiao/FFAdditions.git", :tag => s.version }
s.source_files = "FFAdditions", "Additions/**/*.{h,m}"
s.requires_arc = true
end
5.3 3、执行命令 pod spec lint 验证并提交到CocoPods
wuxianlvzhoudeMacBook-Pro:Additions Allison$ pod spec lint
wuxianlvzhoudeMacBook-Pro:Additions Allison$ pod trunk push FFAdditions.podspec
到此大功告成,我们要尝试一下在终端执行 pod search FFAdditions
,如果能搜到,代表你上传git的这个库,支持cocoapods了.但是我自己在操作的时候,悲催的发现执行这个 pod search FFAdditions
命令的时候,出现这个错误
[!] Unable to find a pod with name, author, summary, or description matching 'FFAdditions'
这个时候只需要command+shift+G调出来前往文件夹的窗口,输入~/Library/Caches/CocoaPods/search_index.json文件将search_index.json删除,或者在终端执行rm ~/Library/Caches/CocoaPods/search_index.json
删除成功后再执行pod search,如果此时还是搜不到,
终端输入:pod setup
会出现Setting up CocoaPods master repo
,稍等几十秒,最底下会输出Setup completed
。说明执行pod setup
成功。
如果pod search操作还是搜索失败,建议在终端多执行几次rm ~/Library/Caches/CocoaPods/search_index.json
,然后再执行再执行pod search
,稍等片刻就会出现你所要搜的类库了。
是不是和我们平时在GitHub上看到的一样了呢,小伙伴们,赶快动手试一下吧.😄
框架的优化
上面我们已经将框架发布到cocoapods上了,但是我们在控制台搜索AFNetworking的时候发现,AFN有很多Subspecs,也就是我们可以单独导入某一个子类
-> AFNetworking (3.1.0)
A delightful iOS and OS X networking framework.
pod 'AFNetworking', '~> 3.1.0'
- Homepage: https://github.com/AFNetworking/AFNetworking
- Source: https://github.com/AFNetworking/AFNetworking.git
- Versions: 3.1.0, 3.0.4, 3.0.3, 3.0.2, 3.0.1, 3.0.0, 3.0.0-beta.3,
3.0.0-beta.2, 3.0.0-beta.1, 2.6.3, 2.6.2, 2.6.1, 2.6.0, 2.5.4, 2.5.3, 2.5.2,
2.5.1, 2.5.0, 2.4.1, 2.4.0, 2.3.1, 2.3.0, 2.2.4, 2.2.3, 2.2.2, 2.2.1, 2.2.0,
2.1.0, 2.0.3, 2.0.2, 2.0.1, 2.0.0, 2.0.0-RC3, 2.0.0-RC2, 2.0.0-RC1, 1.3.4,
1.3.3, 1.3.2, 1.3.1, 1.3.0, 1.2.1, 1.2.0, 1.1.0, 1.0.1, 1.0, 1.0RC3, 1.0RC2,
1.0RC1, 0.10.1, 0.10.0, 0.9.2, 0.9.1, 0.9.0, 0.7.0, 0.5.1 [master repo]
- Subspecs:
- AFNetworking/Serialization (3.1.0)
- AFNetworking/Security (3.1.0)
- AFNetworking/Reachability (3.1.0)
- AFNetworking/NSURLSession (3.1.0)
- AFNetworking/UIKit (3.1.0)
其实我们也可以将我们自己的框架分成许多的子类.找到我们自己的框架,新建分类文件夹,步骤如下:
- 使用Xcode打开YFAdditions.podspec文件
将这段话替换成下面的三句 s.source_files = "YFAdditions", "Additions//.{h,m}"*
s.subspec 'NSArray' do |n|
n.source_files = "YFAdditions", "Additions/NSArray/*/.{h,m}"
ends.subspec 'NSAttributedString' do |att|
att.source_files = "YFAdditions", "Additions/NSAttributedString/*/.{h,m}"
ends.subspec 'Runtime' do |r|
r.source_files = "YFAdditions", "Additions/Runtime/*/.{h,m}"
end
- 验证spec文件
wuxianlvzhoudeMacBook-Pro:Additions Allison$ pod spec lint
-> YFAdditions (1.0.1)
Analyzed 1 podspec.
YFAdditions.podspec passed validation.
3.打tag
wuxianlvzhoudeMacBook-Pro:TEST Allison$ git tag 'v1.0.2'
4.把框架推送到git上
wuxianlvzhoudeMacBook-Pro:Additions Allison$ pod trunk push
//出现一下界面代表成功
Validating podspec
-> YFAdditions (1.0.1)
Updating spec repo `master`
....
----------------------------------------------------------------------------
🎉 Congrats
🚀 YFAdditions (1.0.1) successfully published
📅 August 7th, 01:44
🌎 https://cocoapods.org/pods/YFAdditions
👍 Tell your friends!
--------------------------------------------------------------------------------
4.在控制台搜索YFAdditions,出现以下信息代表成功
-> YFAdditions (1.0.1)
YFAdditions框架
pod 'YFAdditions', '~> 1.0.1'
- Homepage: https://github.com/AllisonWangJiaoJiao/YFAdditions
- Source: https://github.com/AllisonWangJiaoJiao/YFAdditions.git
- Versions: 1.0.1, 1.0.0 [master repo]
- Subspecs:
- YFAdditions/NSArray (1.0.1)
- YFAdditions/NSAttributedString (1.0.1)
- YFAdditions/Runtime (1.0.1)
5.安装你自己的库里面的某个分类,例如我需要 pod 'YFAdditions/NSArray', '~> 1.0.1'库,然后在终端执行pod install命令
注:这是可能会出现如下的error信息
[!] Error installing YFAdditions
[!] /usr/local/bin/git clone https://github.com/AllisonWangJiaoJiao/YFAdditions.git /var/folders/25/frh3dbts1mx9121vthcxqfrm0000gn/T/d20170807-14910-1fgegha --template= --single-branch --depth 1 --branch 1.0.1
Cloning into '/var/folders/25/frh3dbts1mx9121vthcxqfrm0000gn/T/d20170807-14910-1fgegha'...
warning: Could not find remote branch 1.0.1 to clone.
fatal: Remote branch 1.0.1 not found in upstream origin
这里是个隐藏很深的bug,因为你打tag的时候没有加"v"所报了这个错,当然如果你使用sourcetree操作的,可能并不会出现(不晓得什么原因),这里只需要重新打下tag,然后在推送到git,再次pod install即可
如果框架中有多个Subspecs,但是你仅仅需要其中的某几个,可以分别写也可以用统一的写法(两种写法效果相同):
方法一:
pod 'YFAdditions/NSArray'
pod 'YFAdditions/NSAttributedString'
方法二:
pod 'YFAdditions/NSArray', :subspecs => ['NSArray','NSAttributedString']