公司会开发多个 App ,这样创建一个公共的 pod 库是很有必要的,索性昨天趁下班时创建了一个最简单的 pod 库,就把过程记下来吧。

注册 pods trunk 账号

这一步没有什么难度,在终端中注册就好了

1
$ pod trunk register houwenjie11@gmail.com 'houwenjie' --description='创建 CocoaPods'

格式为 pod trunk register github 邮箱 ‘github 用户名’ – description=‘描述’
接下来就是等待 CocoaPods 给你的邮箱发送确认邮件,点击注册成功,在终端中输入 pod trunk me 可以检验成功与否。值得一提的是。。等待了一个小时这封邮件还没有发送过来,所以这一步可能是需要看人品的。

1
2
3
4
5
6
7
8
9
$ pod trunk me
- Name: eassy
- Email: houwenjie11@gmail.com
- Since: February 3rd, 04:50
- Pods:
- HJTestPods
- Sessions:
- February 3rd, 04:50 - June 11th, 20:32. IP: 223.223.202.229
Description: 创建CocoaPods

github 上创建项目

在 github 上创建项目,名字与库的名字相同,不需要生成 readme 和 ignore,也不需要 clone,将项目的地址记录下来就成。

本地创建 repospec 库

创建本地库

这一步可以使用 Cocoapods 提供的一个工具或者叫命令行

1
$ pod lib create HJTestPods

HJTestPods 就是库的名称,执行命令时会让你回答五个问题,分别是使用 1.哪种语言 2.是否需要一个 demo 3.选择一个测试框架 4.是否基于 View 测试 5.类的前缀,我选择的分别是 1.ObjC 2.Yes 3.SPecta 4.Yes 5.HJ。然后他自己会创建项目并生成依赖。
这一步会在当前文件夹下面生成一个 HJTestPods 文件夹,里面盛放的就是 Spec Repo 需要的文件目录等,里面有 redeme ignore 等等。

将本地库注册到 CocoaPods

在终端中执行下面命令,将本地库注册到了 CocoaPods

1
2
# pod repo add 库名称 github地址
$ pod repo add HJTestPods https://github.com/eassy/HJTestPods.git

编辑 podspec 配置文件

podspec 文件中,记录着库的名称,版本,主页,作者,代码地址等信息,这些参数有些已经定义好了,有些需要去自定义。下面是我创建的库的 podspec 文件内容:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#
# Be sure to run `pod lib lint HJTestPods.podspec' to ensure this is a
# valid spec before submitting.
#
# Any lines starting with a # are optional, but their use is encouraged
# To learn more about a Podspec see http://guides.cocoapods.org/syntax/podspec.html
#

Pod::Spec.new do |s|
s.name = 'HJTestPods' ///库的名称
s.version = '0.1.0' ///库的版本,以后升级库的版本号就是编辑这里
s.summary = 'Just Testing' ///库的描述

# 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
TODO: Add long description of the pod here. ///长篇描述
DESC

s.homepage = 'https://github.com/eassy/HJTestPods' ///库的主页,一般都是 github 的项目主页,也可以写成自己的
# s.screenshots = 'www.example.com/screenshots_1', 'www.example.com/screenshots_2'
s.license = { :type => 'MIT', :file => 'LICENSE' } ///默认生成,必须为 MIT
s.author = { 'houwenjie_11@163.com' => 'houwenjie11@gmail.com' } ///库的作者
s.source = { :git => 'https://github.com/eassy/HJTestPods.git', :tag => s.version.to_s } ///库的代码地址
# s.social_media_url = 'https://twitter.com/<TWITTER_USERNAME>'

s.ios.deployment_target = '7.0' ///最低支持版本

s.source_files = 'HJTestPods/Classes/**/*' ///默认生成

# s.resource_bundles = {
# 'HJTestPods' => ['HJTestPods/Assets/*.png']
# }

# s.public_header_files = 'Pod/Classes/**/*.h'
# s.frameworks = 'UIKit', 'MapKit'
# s.dependency 'AFNetworking', '~> 2.3'
end

上面必须要修改的是 s.source s.author s.homepage s.ios.deployment

绑定 git 地址并上传 tag

这步也比较简单

1
2
3
4
5
6
7
$ git add .
$ git commit -s -m "Initial Commit of Library"
$ git remote add origin github地址
$ git push origin master
# 还需要打 tag 并上传
$ git tag -m "first release" "0.1.0"
# git push --tags

验证 podspec 的正确性

1
$ pod lib lint

假如看到

1
2
3
4
 -> HJTestPods
-> HJTestPods (0.2.0)

HJTestPods passed validation.

就说明验证通过了,可以下一步上传

将自己的库上传到 pod

1
$ pod trunk push cocoaPodsName.podspec

这一步有可能时间会久一些,成功以后会更新本地的 pod 库。成功后即可开始使用了。