使用Cocoapods快速创建自己的podspec,让你的框架支持cocoapods,podspec

2016-11-28 13:21:44来源:oschina作者:kingly09人点击



# 使用Cocoapods快速创建自己的podspec,让你的框架支持cocoapods,podspec
## Cocoapods简介
当你开发iOS应用时,会经常使用到很多第三方开源类库,比如JSONKit,AFNetWorking等等。如果使 用他们,传统的方法是,在git上把他们下载下来,然后去配置。这个工作很繁琐,而且也容易出错。不 过有了Cocoapods你就会从这些繁琐的工作中解脱出来。
> Cocoapods是Swif和Objective-C的cocoa项目依赖关系管理的工具。
## 安装CocoaPods
Ruby默认源有改变,下面有介绍:
#### 移除现有Ruby默认源
```
$gem sources --remove https://rubygems.org/
```
#### 使用新的源
```
$gem sources --add /2014th7cj/d/file/p/20161128/nokkwfs14ad --remove https://rubygems.org/
```
注意: 如果之前用的淘宝`Ruby默认源`的话 `https://ruby.taobao.org/` 请求重定向到 `Ruby China` 镜像 ,详细请见:[https://ruby.taobao.org/](https://ruby.taobao.org/)
有关 最新 `RubyGems 镜像- Ruby China` ,请见 [/2014th7cj/d/file/p/20161128/nokkwfs14ad](/2014th7cj/d/file/p/20161128/nokkwfs14ad)
#### 验证新源是否替换成功
```
$gem sources -l https://gems.ruby-china.org
```
确保只有 gems.ruby-china.org
```
$gem install rails
```
#### 执行安装CocoaPods命令
```
$sudo gem install cocoapods
```
备注:苹果系统升级 OS X EL Capitan 后改为
```
$sudo gem install -n /usr/local/bin cocoapods--pre
```
初始化
```
$pod setup
```
#### 更新gem
```
$sudo gem update --system
```#### 验证是否安装成功```
$pod search 第三方 ,例如:执行 $pod search KYBarrageKit
```
出现如下所示,即表明您安装成功了,请愉快的玩Cocoapods了!
```
kinglydeMacBook-Air:~ kingly$ pod search KYBarrageKit
Creating search index for spec repo 'master'.. Done!
-> KYBarrageKit (0.0.2)
KYBarrageKit this is a high availability, easy to use barrage Framework Library.
pod 'KYBarrageKit', '~> 0.0.2'
- Homepage: https://github.com/kingly09/KYBarrageKit
- Source: https://github.com/kingly09/KYBarrageKit.git
- Versions: 0.0.2, 0.0.1 [master repo]
(END)
```
安装OK啦!### 以下是我用以前的安装流程安装时出现的一些错误以及解决方法,供大家参考。
>
> **错误1:**
>Error fetching /2014th7cj/d/file/p/20161128/bnhvsln3avu.org:
> bad response Not Found 404 (/2014th7cj/d/file/p/20161128/bnhvsln3avu.orgspecs.4.8.gz)
> **解决方案:** 把安装流程中 $gem sources -a /2014th7cj/d/file/p/20161128/bnhvsln3avu.org ---改为----> $gem sources -a /2014th7cj/d/file/p/20161128/nokkwfs14ad> **错误2:**
> ERROR:While executing gem ... (Errno::EPERM)
> Operation not permitted - /usr/bin/pod
> **解决方案:** 苹果系统升级OS X EL Capitan后会出现的插件错误,将安装流程 4.安装CocoaPods 的 (1)`sudo gem install cocoapods` ——>改为`sudo gem install -n /usr/local/bin cocoapods`> **错误3:**
> [!] Unable to satisfy the following requirements: - `AVOSCloud (~> 3.1.6.3)` required by `Podfile`
>Specs satisfying the `AVOSCloud (~> 3.1.6.3)` dependency were found, but they required a higher minimum deployment target.
> **解决方案:** 安装流程:`Podfile`文件 中 `platform:ios, ‘6.0’ ` 后边的 6.0 是平台版本号 ,一定要加上> **错误4:**
> podfile升级之后到最新版本为1.0.1后,pod里的内容必须明确指出所用第三方库的target,否则会出现The dependency `` is not used in any concrete target这样的错误。
> **解决方案:**(详细请见 [/2014th7cj/d/file/p/20161128/ykf5l4uzo4k.html)) 错误5:
> [!] The spec did not pass validation, due to 1 error.
> /Library/Ruby/Gems/2.0.0/gems/cocoapods-1.0.1/lib/cocoapods/command/spec/lint.rb:77:in `run'
/Library/Ruby/Gems/2.0.0/gems/claide-1.0.0/lib/claide/command.rb:334:in `run'
/Library/Ruby/Gems/2.0.0/gems/cocoapods-1.0.1/lib/cocoapods/command.rb:50:in `run'
/Library/Ruby/Gems/2.0.0/gems/cocoapods-1.0.1/bin/pod:55:in `'
/usr/local/bin/pod:23:in `load'
/usr/local/bin/pod:23:in `'
kinglydeMacBook-Air:LBinTabBarController kingly$ pod spec lint LBTab.podspec --allow-warnings
>-> LBTab (0.0.2)
- ERROR | [iOS] unknown: Encountered an unknown error (Simulator iPhone 4s is not available.) during validation.> **解决方案:**出现 unknown: Encountered an unknown error (Simulator iPhone 4s is not available.) during validation 因为的 `cocoapods`的版本低了,需要 `cocoapods`的版本为`cocoapods-1.1.0.rc.2`以上
> 执行:`sudo gem install cocoapods --pre`即可。安装的问题完了,接下来怎么创建自己的的podspec了
## 创建自己的podspec详细
让自己的工程支持cocoapods 其实很简单,只需要按照下面几步操作即可。
### 代码提交到github平台,建立git仓库
Cococapods是基于git的,所以我们需要使用git来做版本管理,我使用的是github的免费服务,将自己的代码上传到 github **这里不是唯一的,上传到任何平台上都可以**,前提是项目是开源的至于怎么将自己的项目提交到github上,代码使用github平台管理,这里不做详细讲解,请另查阅资料,网上有很多相关资料与介绍。
建立git仓库,如下图所示:![](/2014th7cj/d/file/p/20161128/dn1fdabeehz.jpg)### 创建.podspec文件
然后cd到你项目的目录,执行命令
```
kinglydeMacBook-Air:Lbin kingly$ cd KYBarrageKit
kinglydeMacBook-Air:KYBarrageKit kingly$ pwd
/Users/kingly/Documents/demo/Lbin/KYBarrageKit
kinglydeMacBook-Air:KYBarrageKit kingly$ ls
KYBarrageKit LICENSE images
KYBarrageKit.podspec README.md
KYBarrageKitDemo README_cn.md
kinglydeMacBook-Air:KYBarrageKit kingly$
```
执行命令
```
// 注 KYBarrageKit 这个是你框架的名称
$ pod spec create KYBarrageKit
```
在 当前目录会显示 一个叫 `KYBarrageKit.podspec` 的文件,即创建成功,当然你也可以直接使用Vim命令创建,只要创建就可以了。
### 编辑.podspec编辑
创建好后打开, 前面有#的为注释,删掉无用的注释(这个不是必须的,为了规范还是删掉哟)
```
kinglydeMacBook-Air:Lbin kingly$ vim KYBarrageKit.podspec
```
内容如下:
```
Pod::Spec.new do |s|
s.name= "KYBarrageKit"
s.summary= "KYBarrageKit this is a high availability, easy to use barrage Framework Library."
s.version= '0.0.2'
s.homepage = "https://github.com/kingly09/KYBarrageKit"
s.license= { :type => "MIT", :file => "LICENSE" }
s.author= { "kingly" => "libintm@163.com" }
s.platform = :ios, "7.0"
s.source= { :git => "https://github.com/kingly09/KYBarrageKit.git", :tag => s.version.to_s }
s.social_media_url = "https://github.com/kingly09"
s.source_files = 'KYBarrageKit/*.{h,m}'
s.framework= "UIKit"
s.requires_arc = true
end
```
那么,每一行是神马意思尼?,大家不要急,接下来讲解一下每行代码的含义:
> s.name:名称,pod search 搜索的关键词,注意这里一定要和.podspec的名称一样,否则报错
>
> s.version:版本号
>
> s.ios.deployment_target:支持的pod最低版本
>
> s.summary: 简介
>
> s.homepage:项目主页地址
>
> s.license:许可证
>
> s.author:作者
>
> s.social_media_url:社交网址,这里我写的微博默认是Twitter,如果你写Twitter的话,你的 podspec发布成功后会[@你](https://my.oschina.net/u/157456)
>
> s.source:项目的地址
>
> s.source_files:需要包含的源文件
>
> s.public_header_files公共的头文件地址
>
> s.resources: 资源文件
>
> s.requires_arc: 是否支持ARC
>
> s.framework= "UIKit" 所需的framework
>
> s.frameworks = 'MobileCoreServices', 'CFNetwork', 'CoreGraphics'所需的framework,多个用逗号隔开
> s.libraries 依赖的动态库,多个用逗号隔开
>
> s.dependency 'AFNetworking', '~> 2.3' 依赖关系,该项目所依赖的其他库,如果有多个需要填写多个s.dependency
>
>
>
podspec文件的详细说明可以看 [Podspec Syntax Reference](http://guides.cocoapods.org/syntax/podspec.html)
### 创建LICENSE(许可证/授权)文件
这个文件是必须要的,当建立git仓库的时候,不需要选择 `MIT` 协议,自动会生成 `LICENSE` 文件,如果没有手动创建,工程目录如下图所示:![](/2014th7cj/d/file/p/20161128/gwfcmqbziaz.jpg)LICENSE 内容如下,只需要把前面的版权改一下就行了,后面的都一样:
```
MIT License
Copyright (c) 2016 KYBarrageKitSoftware (https://github.com/kingly09/KYBarrageKit) by kingly inc.
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
```
### 使用Git上传到github
将包含配置好的 `.podspec`, `LICENSE` 的项目提交 `github` 上
### 打tag
> 因为`cocoapods`是依赖`tag`版本的,所以必须打`tag`,
> 以后再次更新只需要把你的项目打一个`tag`
> 然后修改`.podspec`文件中的版本接着提交到`cocoapods`官方就可以了,提交命令请看下面```
// 为git打tag
$git tag 0.0.3
// 将tag推送到远程仓库
$git push --tags
```
### 验证.podspec文件
到此检查一下你工程下面的文件, 你的项目, `.podspec`文件, `LICENSE`文件是否存在;
验证会先测试本地 `.podspec` 文件是否存在语法错误.
然后执行命令
```
// --verbose 如果验证失败会报错误信息
pod spec lint KYBarrageKit.podspec --verbose
```
验证过程中:
```
-> KYBarrageKit
```
出现如下面的字样,证明验证成功:```
KYBarrageKit.podspec passed validation.
```### 注册Trunk
trunk需要CocoaPods 0.33版本以上,用 `pod --version`命令查看版本
如果版本低,需要升级:
```
sudo gen install cocoapods
pod setup
```
已经注册过的不需要注册,怎么看自己有没有注册,执行下面的命令:
```
pod trunk me
```
下面是我的注册信息:
```
kinglydeMacBook-Air:KYBarrageKit kingly$ pod trunk me
- Name: kingly09
- Email:libintm@163.com
- Since:September 23rd, 03:34
- Pods:
- KYBarrageKit
- Sessions:
- September 23rd, 03:34 - February 25th, 20:28. IP: 113.116.78.56Description:
macbook air
- September 25th, 22:46 - April 4th, 03:26. IP: 183.17.252.175
kinglydeMacBook-Air:KYBarrageKit kingly$
```
如果没有注册过,执行
```
pod trunk register libintm@163.com "kingly09" --verbose
```
> 注册完成之后会给你的邮箱发个邮件,进入邮箱邮件里面有个链接,需要点击确认一下
>
### 发布
发布时会验证 Pod 的有效性,如果你在手动验证 Pod 时使用了 `--use-libraries` 或 `--allow-warnings` 等修饰符,那么发布的时候也应该使用相同的字段修饰,否则出现相同的报错。
```
// --use-libraries --allow-warnings
pod trunk push KYBarrageKit.podspec
```
验证中
```
Validating podspec
-> KYBarrageKit (0.0.3)
```
发布成功
```
kinglydeMacBook-Air:KYBarrageKit kingly$ pod trunk push KYBarrageKit.podspec
Updating spec repo `master`
CocoaPods 1.2.0.beta.1 is available.
To update use: `sudo gem install cocoapods --pre`
[!] This is a test version we'd love you to try.
For more information, see /2014th7cj/d/file/p/20161128/ebbikax4iyb.org and the CHANGELOG for this version at https://github.com/CocoaPods/CocoaPods/releases/tag/1.2.0.beta.1
Validating podspec
-> KYBarrageKit (0.0.3)
Updating spec repo `master`
CocoaPods 1.2.0.beta.1 is available.
To update use: `sudo gem install cocoapods --pre`
[!] This is a test version we'd love you to try.
For more information, see /2014th7cj/d/file/p/20161128/ebbikax4iyb.org and the CHANGELOG for this version at https://github.com/CocoaPods/CocoaPods/releases/tag/1.2.0.beta.1
- Data URL: https://raw.githubusercontent.com/CocoaPods/Specs/9d4bb18a14436a82c5487c73e13283561db549c0/Specs/d/f/4/KYBarrageKit/0.0.3/KYBarrageKit.podspec.json
- Log messages:
- November 27th, 03:43: Push for `KYBarrageKit 0.0.3' initiated.
- November 27th, 03:43: Push for `KYBarrageKit 0.0.3' has been pushed (0.164832082 s).
kinglydeMacBook-Air:KYBarrageKit kingly$
```### 最后赶快验证一下
```
pod search KYBarrageKit
```
出现如下信息,即大功告成了!```
-> KYBarrageKit (0.0.3)
KYBarrageKit this is a high availability, easy to use barrage Framework
Library.
pod 'KYBarrageKit', '~> 0.0.3'
- Homepage: https://github.com/kingly09/KYBarrageKit
- Source: https://github.com/kingly09/KYBarrageKit.git
- Versions: 0.0.3, 0.0.2, 0.0.1 [master repo]
(END)
```# 扩展阅读* [Podspec Syntax Reference](http://guides.cocoapods.org/syntax/podspec.html)
* [Developing private static library for iOS with CocoaPods](http://blog.sigmapoint.pl/developing-static-library-for-ios-with-cocoapods/)
* [Automatic build of static library for iOS and many architectures](http://blog.sigmapoint.pl/automatic-build-of-static-library-for-ios-for-many-architectures/)
* [Avoiding dependency collisions in iOS static library managed by CocoaPods](http://blog.sigmapoint.pl/avoiding-dependency-collisions-in-ios-static-library-managed-by-cocoapods/)

最新文章

123

最新摄影

微信扫一扫

第七城市微信公众平台