Notes and thoughts from Tony

创建自己的 pod

Comments

前言

估计这是下个版本间歇起的最后一篇了吧。项目上需要一个图片选择控件,找到一个 swift 项目,花了两天时间转成了 OC 项目,然后想做个 pod 分享给大家。

创建项目

首先需要在 github 上创建新的项目,以当前项目为例:

pod01

然后 clone 到本地,进行源码和资源的添加。再 commit 并 push 到 master 分支上。这些都是 git 相关,不细介绍。

附开源协议介绍,一般项目选择 MIT 就好:

pod03

需要注意的是,发布 pod 需要有 tag 版本。所以要打个 tag 并进行 push。

# hanxt @ Hanxt in ~/Documents/GitHubWorkspace/ImagePicker-Objective-C on git:master o [16:53:33]
$ git tag 0.0.1

# hanxt @ Hanxt in ~/Documents/GitHubWorkspace/ImagePicker-Objective-C on git:master o [16:53:35]
$ git push --tags
Username for 'https://github.com': tonyh2021
Password for 'https://tonyh2021@github.com':
Total 0 (delta 0), reused 0 (delta 0)
To https://github.com/tonyh2021/ImagePicker-Objective-C.git
 * [new tag]         0.0.1 -> 0.0.1

pod 准备

首先注册 pod,类似注册账号:

# hanxt @ Hanxt in ~ [16:16:15]
$ pod trunk register xxx@xxx 'TonyHan'
[!] Please verify the session by clicking the link in the verification email that has been sent to xxx@xxx

进入邮箱后点击链接确认,返回命令行确认:

# hanxt @ Hanxt in ~ [16:16:36]
$ pod trunk me
  - Name:     TonyHan
  - Email:    xxx@xxx
  - Since:    September 23rd, 02:16
  - Pods:     None
  - Sessions:
    - September 23rd, 02:16 - January 29th, 2017 02:17.

创建 podspec 文件

# hanxt @ Hanxt in ~ [16:17:35]
$ pod spec create ImagePicker-Objective-C

Specification created at ImagePicker-Objective-C.podspec

该文件是 ruby 文件,里面的条目都很容易知道含义。其中需要说明的又几个参数:

  1. s.license : Pods 依赖库使用的 license 类型,大家填上自己对应的选择即可。
  2. s.source_files : 表示源文件的路径,注意这个路径是相对 podspec 文件而言的。
  3. s.frameworks : 需要用到的 frameworks,不需要加 .frameworks 后缀。

可以参考我的文件 ImagePicker-Objective-C.podspec

pod 验证

为 pod 添加版本号并打上 tag (我没有发现执行后有什么变化😅):

# hanxt @ Hanxt in ~/Documents/GitHubWorkspace/ImagePicker-Objective-C on git:master o [16:56:54]
$ set the new version to 0.0.1

# hanxt @ Hanxt in ~/Documents/GitHubWorkspace/ImagePicker-Objective-C on git:master o [16:56:56]
$ set the new tag to 0.0.1

执行验证命令:

# hanxt @ Hanxt in ~/Documents/GitHubWorkspace/ImagePicker-Objective-C on git:master o [16:57:06]
$ pod lib lint

xcode-select: error: tool 'xcodebuild' requires Xcode, but active developer directory '/Library/Developer/CommandLineTools' is a command line tools instance
 -> ImagePicker-Objective-C (0.0.1)
    - ERROR | license: Sample license type.
    - ERROR | description: The description is empty.
    - ERROR | [iOS] unknown: Encountered an unknown error (The `ImagePicker-Objective-C` pod failed to validate due to 2 errors:
    - ERROR | license: Sample license type.
    - ERROR | description: The description is empty.

) during validation.

[!] ImagePicker-Objective-C did not pass validation, due to 3 errors.
You can use the `--no-clean` option to inspect any issue.

有错,license 和 description 有问题,改掉。但是 xcode-select: error: tool 'xcodebuild' requires Xcode 这个错误呢?stackoverflow 说是大概找不到这个工具了,我突然想起来昨天我把 Xcode 改名为 Xcode7 和 Xcode8 ,估计是命令的链接找不到了。把名字改回 Xcode,再次验证,成功。

# hanxt @ Hanxt in ~/Documents/GitHubWorkspace/ImagePicker-Objective-C on git:master x [16:59:21] C:1
$ pod lib lint

 -> ImagePicker-Objective-C (0.0.1)

Demo

提供供别人参考的 Demo 是更优雅的做法。把之前的测试代码拉过来,层级结构如图:

pod02

pod 文件如下:

platform :ios, "8.0"

inhibit_all_warnings!

target "ImagePickerDemo" do
    pod 'ImagePicker-Objective-C', path: '../../'
end

install 之后需要将之前控件源码删掉,用我们的 pod 中的控件。Build 无问题,运行无问题。

资源问题

上传 podspec 文件

要想一个 pod 依赖库真正可用,还需要做最后一步操作,将我们刚才生成的 podspec 文件上传到 CocoaPods 官方的 Specs 仓库中,才能成为一个真正的 pod 依赖库,供小伙伴们使用。

fork 并 clone Specs 仓库(时间会比较久啦,其实我在用 github 客户端☺️)。

podspec 文件在 Specs 仓库中的保存原则是:

pod 依赖库同名文件夹 -> 版本号同名文件夹 -> podspec文件

OK,现在我们可以上传…

半小时后,我决定直接向 github 上传我的文件😈。研究了下,貌似没有上传或者新建目录的功能😂。

后来发现其他的方法,直接使用 pod 命令。估计不需要每次都用 git 来同步,试试吧。

在项目目录下:

pod trunk push

[!] Found podspec `ImagePicker-Objective-C.podspec`
[!] You need to register a session first.

我换了家里的电脑,不同的电脑终端也需要认证,于是:

# BloodLine @ BloodLineMac in ~/Documents/githubworkspace/ImagePicker-Objective-C on git:master o [11:18:57] C:1
$ pod trunk register xxx@163.com 'Tony Han' --description='macbook pro'

# BloodLine @ BloodLineMac in ~/Documents/githubworkspace/ImagePicker-Objective-C on git:master o [11:20:34]
$ pod trunk me
  - Name:     TonyHan
  - Email:    xxx@163.com
  - Since:    September 23rd, 02:16
  - Pods:     None
  - Sessions:
    - September 23rd, 02:16 - January 29th, 2017 02:17. IP: 211.103.xxx.xxx
    - September 23rd, 21:20 - January 29th, 2017 21:21. IP: 223.72.xxx.xxx
    Description: macbook pro

认证完成,执行:

$ pod trunk push --verbose

[!] Found podspec `ImagePicker-Objective-C.podspec`
Updating spec repo `master`
# 花了很长时间...
  - Data URL: https://raw.githubusercontent.com/CocoaPods/Specs/201be904f9c457f2d69ed84359151b6e0063e249/Specs/ImagePicker-Objective-C/0.0.1/ImagePicker-Objective-C.podspec.json
  - Log messages:
    - September 23rd, 22:07: Push for `ImagePicker-Objective-C 0.0.1' initiated.
    - September 23rd, 22:07: Push for `ImagePicker-Objective-C 0.0.1' has been pushed
    (0.416402311 s).

参考:

CocoaPods详解之—-制作篇

如何发布自己的开源框架到CocoaPods

代码:

文章中的代码都可以从我的GitHub ImagePicker-Objective-C找到。