3分钟让你的框架支持cocoapods,podspec文件讲解

军哥昨天搞了半天终于让自己的框架支持cocoapods了,下面就上教程,方便朋友学习,让自己的框架支持cocoapods

3分钟快速搞定cocoapods

其实让自己的工程支持cocoapods很简单只需要几步
1.创建.podspec
2.编辑.podspec
3.将自己的项目打成tag
4.验证
5.注册CocoaPods
6.发布

1.代码提交到github平台

将自己的代码上传到github这里不是唯一的,上传到任何平台上都可以,前提是项目是开源的至于怎么将自己的项目提交到github上,稍后会上教程

我自己的项目

2.创建.podspec

然后cd到你项目的目录,执行命令,你也可以使用vim创建,只要创建就可以了

// 注 YJSettingTableView 这个是你框架的名称
$ pod spec create YJSettingTableView

3.编辑.podspec

创建好后打开,删除注释, 前面有#的为注释,如果你想知道每个东西的含义可以了解一下
整理之后的文件

Pod::Spec.new do |s|
    s.name         = "YJSettingTableView"
    s.version      = "1.0.0"
    s.ios.deployment_target = '7.0'
    s.summary      = "A delightful setting interface framework."
    s.homepage     = "https://github.com/coderYJ/YJSettingTableView"
    s.license              = { :type => "MIT", :file => "LICENSE" }
    s.author             = { "coderYJ" => "[email protected]" }
    s.social_media_url   = "http://weibo.com/u/5348162268"
    s.source       = { :git => "https://github.com/coderYJ/YJSettingTableView.git", :tag => s.version }
    s.source_files  = "YJSettingTableView/*.{h,m}"
    s.resources          = "YJSettingTableView/YJSettingTableView.bundle"
    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发布成功后会@你
s.source:项目的地址
s.source_files:需要包含的源文件
s.resources: 资源文件
s.requires_arc: 是否支持ARC
s.dependency:依赖库,不能依赖未发布的库
s.dependency:依赖库,如有多个可以这样写
例如

s.dependency  = 'AFNetworking'

s.license= { :type => "MIT", :file => "LICENSE" }
这里建议大家这样写,如果写别的会报警告,导致后面一直提交失败,这里军哥已经跳了很多坑

  • source_files:写法及含义建议大家写第一种或者第二种
"YJSettingTableView/*
""YJSettingTableView/YJSettingTableView/*.{h,m}"
"YJSettingTableView/**/*.h"
  • “*” 表示匹配所有文件

  • “*.{h,m}” 表示匹配所有以.h和.m结尾的文件

  • “**” 表示匹配所有子目录

  • s.source 常见写法

s.source = { :git => "https://github.com/coderYJ/YJSettingTableView.git", :commit => "68defea" }
s.source = { :git => "https://github.com/coderYJ/YJSettingTableView.git", :tag => 1.0.0 }
s.source = { :git => "https://github.com/coderYJ/YJSettingTableView.git", :tag => s.version }
  • commit => "68defea" 表示将这个Pod版本与Git仓库中某个commit绑定
  • tag => 1.0.0 表示将这个Pod版本与Git仓库中某个版本的comit绑定
  • tag => s.version 表示将这个Pod版本与Git仓库中相同版本的comit绑定

4.创建LICENSE(许可证/授权)文件,此文件必须要有

军哥在这里被坑过,创建一个文件名字命名为LICENSE,内容为:
只需要把前面的版权改一下就行了,后面的都一样

Copyright (c) 2011-2016 YJSettingTableView Software Foundation (https://github.com/coderYJ/YJSettingTableView/)

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.

5.上传到Git

将包含配置好的 .podspec, LICENSE 的项目提交 Git

6.打tag

因为cocoapods是依赖tag版本的,所以必须打tag,
以后再次更新只需要把你的项目打一个tag
然后修改.podspec文件中的版本接着提交到cocoapods官方就可以了,提交命令请看下面

  • 执行命令
//为git打tag, 第一次需要在前面加一个v
git tag "v1.0.0" 
//将tag推送到远程仓库
git push --tags 

7.验证.podspec文件

  • 到此检查一下你工程下面的文件, 你的项目, .podspec文件, LICENSE文件
  • 验证会先测试本地 .podspec 文件是否存在语法错误.


    文件目录

    然后执行命令

// --verbose 如果验证失败会报错误信息
pod spec lint YJSettingTableView.podspec --verbose

验证过程中:

-> YJSettingTableView

验证成功后:

YJSettingTableView.podspec passed validation.
如图

验证失败:

[!] The spec did not pass validation, due to 1 error.

这个过程会出现很多错误,这里不一一列举,大家按照错误信息自行修改,不过一般不会出现错误,只要你按照军哥说的做,基本上是没有问题的,因为军哥走过的坑已经告诉你们了

8.注册Trunk

trunk需要CocoaPods 0.33版本以上,用pod --version
命令查看版本,

  • 如果版本低,需要升级:
sudo gen install cocoapods
pod setup
  • 已经注册过的不需要注册,怎么看自己有没有注册
pod trunk me
  • 我的注册信息
-Name:     coderYJ
  - Email:    wenyanjun1314@163.com
  - Since:    August 12th, 04:37
  - Pods:
    - YJSettingTableView
  - Sessions:
    - August 12th, 04:37 -           Unverified. IP: 113.111.64.45
    Description: macbook pro
    - August 12th, 04:39 - December 18th, 21:11. IP: 113.111.64.45
    Description: macbook pro
  • 注册
// 加上--verbose可以输出详细debug信息,方便出错时查看。
pod trunk register wenyanjun1314@163.com "coderYJ" --verbose

注册完成之后会给你的邮箱发个邮件,进入邮箱邮件里面有个链接,需要点击确认一下

9.发布

发布时会验证 Pod 的有效性,如果你在手动验证 Pod 时使用了 --use-libraries 或 --allow-warnings 等修饰符,那么发布的时候也应该使用相同的字段修饰,否则出现相同的报错。

// --use-libraries --allow-warnings
pod trunk push YJSettingTableView.podspec 
  • 验证中


    正在验证
  • 发布成功


    发布成功
  • 发布成功后的信息,终端中的信息

bogon:YJSettingTableView simplyou$ pod trunk push YJSettingTableView.podspec 
Updating spec repo `master`
CocoaPods 1.1.0.beta.1 is available.
To update use: `gem install cocoapods --pre`
[!] This is a test version we'd love you to try.
For more information see http://blog.cocoapods.org
and the CHANGELOG for this version http://git.io/BaH8pQ.
Validating podspec
[!] Unable to load a specification for the plugin `/Library/Ruby/Gems/2.0.0/gems/cocoapods-deintegrate-1.0.0`
 -> YJSettingTableView (1.0.0)
[!] Unable to accept duplicate entry for: YJSettingTableView (1.0.0)
[!] Unable to read the license file `/private/var/folders/qr/x09stzhx4rvf8cd4s0nmw8m80000gn/T/CocoaPods/Lint/Pods/YJSettingTableView/LICENSE` for the spec `YJSettingTableView (1.0.0)`
[!] Unable to read the license file `/private/var/folders/qr/x09stzhx4rvf8cd4s0nmw8m80000gn/T/CocoaPods/Lint/Pods/YJSettingTableView/LICENSE` for the spec `YJSettingTableView (1.0.0)`

10.最后一步赶快验证一下

pod search YJSettingTableView
  • 会出现如下信息
bogon:YJSettingTableView simplyou$ pod search YJSettingTableView
-> YJSettingTableView (1.0.0)
   A delightful setting interface framework.
   pod 'YJSettingTableView', '~> 1.0.0'
   - Homepage: https://github.com/coderYJ/YJSettingTableView
   - Source:   https://github.com/coderYJ/YJSettingTableView.git
   - Versions: 1.0.0 [master repo]
bogon:YJSettingTableView simplyou$ 

到此大功告成,小伙伴们赶快让你的框架支持pod吧
如果出现问题,欢迎咨询军哥,定期更新错误,大家相互学习
请关注我,联系方式在我的简书首页
持续更新实用的干货



作者:coderYJ
链接:https://www.jianshu.com/p/8a7b9232cbab
來源:简书
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。

猜你喜欢

转载自blog.csdn.net/zxtech_lxy/article/details/80283293