使用Cocoapods托管代码

一. 新建项目

#######1.1: 在Github上新建项目, 新建的时候要勾选许可类型:

如果是已有的项目, 可以从其他地方copy一个 LICENSE文件, 放到项目的根目录下(与READEME.md同级)

#######1.2: 使用Sourcetree(命令行), 将项目克隆到本地:
git clone https://github.com/SmallTwoInn/XZSelectView.git /Users/mac/Desktop/XZSelectView

#######1.3: 新建一个项目, 随便添加一个文件( 你需要支持cocoapods的文件, 这里以LZTool类为例 ):

#######1.4: 推送到Github:



关于Cocoapods的使用, 可参考这篇文章:[Cocoapods]第三方管理工具Cocoapods使用

二. 创建podspec文件

2.1: 打开终端, 进入到项目的根目录:
cd /Users/mac/Desktop/PodTest
2.2: 然后执行下面的指令新建Podspec文件:
pod spec create LZTool

这里的LZTool, 就是你在使用cocoapods时写到Podfile文件内的名称;

pod spec create 文件名称
2.3: 打开podspec文件

此时, 本地文件夹下就多了一个文件: LZTool.podspec
在终端, 打开这个文件(或者使用其他的方式打开, 例如: 文本编辑器, Xcode等 ):
打开后, 会发现里面有很多内容, 如下 :

#
#  Be sure to run `pod spec lint LZTool.podspec' to ensure this is a
#  valid spec and to remove all comments including this before submitting the spec.
#
#  To learn more about Podspec attributes see http://docs.cocoapods.org/specification.html
#  To see working Podspecs in the CocoaPods repo see https://github.com/CocoaPods/Specs/
#

Pod::Spec.new do |s|

  # ―――  Spec Metadata  ―――――――――――――――――――――――――――――――――――――――――――――――――――――――――― #
  #
  #  These will help people to find your library, and whilst it
  #  can feel like a chore to fill in it's definitely to your advantage. The
  #  summary should be tweet-length, and the description more in depth.
  #

  s.name         = "LZTool"
  s.version      = "0.0.1"
  s.summary      = "A short description of LZTool."

  # This description is used to generate tags and improve search results.
  #   * Think: What does it do? Why did you write it? What is the focus?
  #   * Try to keep it short, snappy and to the point.
  #   * Write the description between the DESC delimiters below.
  #   * Finally, don't worry about the indent, CocoaPods strips it!
  s.description  = <<-DESC
                   DESC

  s.homepage     = "http://EXAMPLE/LZTool"
  # s.screenshots  = "www.example.com/screenshots_1.gif", "www.example.com/screenshots_2.gif"


  # ―――  Spec License  ――――――――――――――――――――――――――――――――――――――――――――――――――――――――――― #
  #
  #  Licensing your code is important. See http://choosealicense.com for more info.
  #  CocoaPods will detect a license file if there is a named LICENSE*
  #  Popular ones are 'MIT', 'BSD' and 'Apache License, Version 2.0'.
  #

  s.license      = "MIT (example)"
  # s.license      = { :type => "MIT", :file => "FILE_LICENSE" }


  # ――― Author Metadata  ――――――――――――――――――――――――――――――――――――――――――――――――――――――――― #
  #
  #  Specify the authors of the library, with email addresses. Email addresses
  #  of the authors are extracted from the SCM log. E.g. $ git log. CocoaPods also
  #  accepts just a name if you'd rather not provide an email address.
  #
  #  Specify a social_media_url where others can refer to, for example a twitter
  #  profile URL.
  #

  s.author             = { "LQQZYY" => "592852848@qq.com" }
  # Or just: s.author    = "LQQZYY"
  # s.authors            = { "LQQZYY" => "592852848@qq.com" }
  # s.social_media_url   = "http://twitter.com/LQQZYY"

  # ――― Platform Specifics ――――――――――――――――――――――――――――――――――――――――――――――――――――――― #
  #
  #  If this Pod runs only on iOS or OS X, then specify the platform and
  #  the deployment target. You can optionally include the target after the platform.
  #

  # s.platform     = :ios
  # s.platform     = :ios, "5.0"

  #  When using multiple platforms
  # s.ios.deployment_target = "5.0"
  # s.osx.deployment_target = "10.7"
  # s.watchos.deployment_target = "2.0"
  # s.tvos.deployment_target = "9.0"


  # ――― Source Location ―――――――――――――――――――――――――――――――――――――――――――――――――――――――――― #
  #
  #  Specify the location from where the source should be retrieved.
  #  Supports git, hg, bzr, svn and HTTP.
  #

  s.source       = { :git => "http://EXAMPLE/LZTool.git", :tag => "#{s.version}" }


  # ――― Source Code ―――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――― #
  #
  #  CocoaPods is smart about how it includes source code. For source files
  #  giving a folder will include any swift, h, m, mm, c & cpp files.
  #  For header files it will include any header in the folder.
  #  Not including the public_header_files will make all headers public.
  #

  s.source_files  = "Classes", "Classes/**/*.{h,m}"
  s.exclude_files = "Classes/Exclude"

  # s.public_header_files = "Classes/**/*.h"


  # ――― Resources ―――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――― #
  #
  #  A list of resources included with the Pod. These are copied into the
  #  target bundle with a build phase script. Anything else will be cleaned.
  #  You can preserve files from being cleaned, please don't preserve
  #  non-essential files like tests, examples and documentation.
  #

  # s.resource  = "icon.png"
  # s.resources = "Resources/*.png"

  # s.preserve_paths = "FilesToSave", "MoreFilesToSave"


  # ――― Project Linking ―――――――――――――――――――――――――――――――――――――――――――――――――――――――――― #
  #
  #  Link your library with frameworks, or libraries. Libraries do not include
  #  the lib prefix of their name.
  #

  # s.framework  = "SomeFramework"
  # s.frameworks = "SomeFramework", "AnotherFramework"

  # s.library   = "iconv"
  # s.libraries = "iconv", "xml2"


  # ――― Project Settings ――――――――――――――――――――――――――――――――――――――――――――――――――――――――― #
  #
  #  If your library depends on compiler flags you can set them in the xcconfig hash
  #  where they will only apply to your library. If you depend on other Podspecs
  #  you can include multiple dependencies to ensure it works.

  # s.requires_arc = true

  # s.xcconfig = { "HEADER_SEARCH_PATHS" => "$(SDKROOT)/usr/include/libxml2" }
  # s.dependency "JSONKit", "~> 1.4"

end

########2.3.1 Spec Metadata: 元数据规范
这些内容是你的类库信息

- s.name : 类库的名称
- s.version : 类库的版本号
- s.summary : 类库的摘要(简介)
- s.description : 描述, 用于生成标签和提高搜索结果, 主要是类库的功能等, 尽量简短; 注意, 需要写在两个DESC中间
- s.homepage : 类库的地址链接
- s.screenshots : 效果图链接

########2.3.2 Spec License: 开源许可证
更多可参考: http://choosealicense.com

- s.license: 许可类型, 常用的是 MIT

########2.3.3 Author Metadata: 作者信息

- s.author :作者
- s.social_media_url : 个人主页地址

########2.3.4 Platform Specifics: 平台信息

- s.platform : 平台 例如: iOS , tvOS, watchOS等,
在平台信息后可以跟上需要的最低版本, 例如:
s.platform = :ios, "8.0"
如果可以支持多个平台, 可使用下面的方式指定:
        s.ios.deployment_target = "5.0"
        s.osx.deployment_target = "10.7"
        s.watchos.deployment_target = "2.0"
        s.tvos.deployment_target = "9.0"

########2.3.5 Source Location: 文件地址
从哪里可以获得这些文件, 支持 git, hg, bzr, svn and HTTP.

- s.source : 链接地址

########2.3.6 Resources: 资源

- s.resource = "icon.png":单个资源
- s.resources = "Resources/*.png" : 多个资源
- s.preserve_paths = "FilesToSave", "MoreFilesToSave" : 需要保存的文件

########2.3.7 Source Code: 共享源文件

- s.source_files : 文件的相对路径
- s.public_header_files : 公开的头文件, 如果不设置这个, 默认公开所有的头文件
- s.exclude_files = "Classes/Exclude" 排除不需要的文件

########2.3.8: Project Linking: 链接库和框架

- s.framework = "SomeFramework" 单个framework
- s.frameworks = "SomeFramework", "AnotherFramework" 多个库framework
- s.library = "iconv" 指定导入的库,比如sqlite3
- s.libraries = "iconv", "xml2" 多个库

########2.3.9: Project Settings: 工程设置

- s.requires_arc = true 需要ARC
- s.xcconfig = { "HEADER_SEARCH_PATHS" => "$(SDKROOT)/usr/include/libxml2" } 项目配置
- s.dependency "JSONKit", "~> 1.4" 需要其他第三方库的支持
2.4: 完成后, 执行下面的指令来验证这个文件是否可用:
pod lib lint LZTool.podspec

如果有错误, 根据错误提示, 进行修改; 如果仅仅是警告, 不想修改的话, 可以使用下面的指令来忽略:

pod lib lint LZTool.podspec —allow-warnings

最后, 输出下面这个, 即是验证通过:


这里如果遇到错误, 可以参考[Cocoapods]项目添加Cocoapods支持遇到的坑中的解决思路.
http://www.jianshu.com/p/283584683b0b

三. 新建版本

3.1: 使用github的界面点击
  • 1: 来到Github 上, 创建release版本选择release :


  • 2: 如果是个新的项目的话, 这里是没有任何版本信息的, 点击create a new release :


  • 3: 填好之后, 点击下面的 Public release 即可完成!


3.2: 使用tag git命令来实现新建版本
  • 1: 创建一个tag
 git tag '0.0.1'  -m '说明'
  • 2: 上传tag
git push --tags  // 上传所有的tags

git push tag 0.0.1 // 上传某一条

注意: 这里的Tag verson 一定要和上面设置的s.version以及 s.source 中的 tag保持一致.

四. 注册cocoapods账户

4.1接下来输入以下指令, 来链接到你的cocoapods账户:

########4.1.1:如果是首次使用:

// 邮箱  用户名   描述信息
pod trunk register 302934443@qq.com 'LQQ' --description='Test1'

########4.1.2: 已经注册过

pod trunk register eloy@example.com --description='Work Laptop'
pod trunk register eloy@example.com

不出错的话会输出:

########4.2: 邮箱验证
根据提示, 到你的邮箱里去确认邮件即可!

这里如果遇到错误, 可以参考[Cocoapods]项目添加Cocoapods支持遇到的坑中的解决思路.

########4.3 确认账号是否可用:

pod trunk me

如果可用, 会输出相关的信息:


五. 上传代码到Cocoapods

以上全部完成后, 最后一步就是上传代码到Cocoapods了;

5.1: 使用下面的指令来检查文件是否可用:
pod spec lint

如果没有错误, 会输出:


PS: 如果有警告, 而我们又不想处理, 可用—allow-warnings忽略, 即:

pod spec lint —allow-warnings

这里如果遇到错误, 可以参考[Cocoapods]项目添加Cocoapods支持遇到的坑中的解决思路.

最后就是上传代码了, 使用下面的指令进行上传:

pod trunk push LZTool.podspec

上传的时间可能会有点长, 耐心等待, 成功后会输出:


PS: 如果这里因为有警告而导致失败, 可以在后面添加--allow-warnings来忽略警告, 即:

pod trunk push LZTool.podspec --allow-warnings

然后, 可用使用下面的指令来查看是否可用:

pod search LZTool

六. 更新

如果我们需要更新自己的类库, 只需要将最新的代码提交到Github, 然后创建新的release版本, 注意: 这里的版本要和 podspec文件内的s.version一致, 然后再执行一遍下面的指令即可:

pod trunk push LZTool.podspec --allow-warnings

最后附上本次实例的 podspec文件内容:

Pod::Spec.new do |s|

  s.name         = "LZTool"
  s.version      = "0.0.1"
  s.summary      = " This is some summary for LZTool"

  s.description  = <<-DESC 
                          LZTool 是一个用于保存一些常用工具类的工具
                   DESC

  s.homepage     = "https://github.com/LQQZYY/LZTool"

  s.license      = "MIT"

  s.author    = "LQQZYY"
  s.platform     = :ios, "8.0"

  s.source       = { :git => "https://github.com/LQQZYY/PodTest.git", :tag => "0.0.1" }
  s.source_files = "LZTool"

  s.requires_arc = true

end

最后, 对于.podspec文件的编写, 可以下载其他的第三方库来学习里面的 .podspec的编写技巧.

http://www.jianshu.com/p/a72a529dc659
http://www.jianshu.com/p/e5209ac6ce6b
http://www.jianshu.com/p/283584683b0b
http://www.jianshu.com/p/9727434520b0
http://www.jianshu.com/p/1139a603f413
http://blog.csdn.net/keleyundou/article/details/49635589
http://www.jianshu.com/p/2f8680192283

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 228,316评论 6 531
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 98,481评论 3 415
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 176,241评论 0 374
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 62,939评论 1 309
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 71,697评论 6 409
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 55,182评论 1 324
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 43,247评论 3 441
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 42,406评论 0 288
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 48,933评论 1 334
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 40,772评论 3 354
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 42,973评论 1 369
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 38,516评论 5 359
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 44,209评论 3 347
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 34,638评论 0 26
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 35,866评论 1 285
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 51,644评论 3 391
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 47,953评论 2 373

推荐阅读更多精彩内容