手把手教你创建自己的Cocoapods库

如果您能看这篇文章,相信您已经了解并喜欢上了 Cocoapods, 下面我们一步一步的创建一个属于自己的 Cocoapods 库.

创建

通过下面命令,可以使用一个创建向导简历 Cocoapods 库

1
pod lib create GUICodeSnippet

然后终端会进行引导,依次选择即可. 回车键确认默认选择.

问题依次是:

  1. 语言

  2. 创建demo工程( 极力推荐创建)

  3. 选择测试框架 (可选择默认,如果非常确定没有需要,可以选择None)

  4. View-based Testing (官方推荐使用 FBSnapShotTestCase)

  5. OC类前缀 (如果语言选择非OC,不会有这项)

打开工程,会发现有一个 GUICodeSnippet 和一个 Pods, 在
Pods->Development Pods->GUICodeSnippet->Pod->Classes中,有一个ReplaceMe.m

你需要将这个文件替换成自己的代码文件.

源码验证

进入 Example 目录 , 执行

1
pod install

然后 Xcode 打开 xcworkspace 文件 ,去跑一下项目,如果没有错误,证明添加的代码没有问题,可以继续进行

准备工作

配置 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
43
44
45
46
47
48
#
# Be sure to run `pod lib lint GUICodeSnippet.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 = "GUICodeSnippet"
s.version = "0.1.8"
s.summary = "code segment of kennyGui"

# 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
code segment of kenny ,and in most situation ,is for private use only
DESC

s.homepage = "https://git.oschina.net/kennygui/GUICodeSnippet"
# s.screenshots = "www.example.com/screenshots_1", "www.example.com/screenshots_2"
s.license = 'MIT'
s.author = { "katsurake" => "katsurake@foxmail.com" }
s.source = { :git => "https://git.oschina.net/kennygui/GUICodeSnippet.git", :tag => s.version.to_s }
# s.social_media_url = 'https://twitter.com/<TWITTER_USERNAME>'

s.platform = :ios, '7.0'
s.requires_arc = true

s.source_files = 'Pod/Classes/*.h'
s.resource_bundles = {
'GUICodeSnippet' => ['Pod/Assets/*.png']
}

s.public_header_files = 'Pod/Classes/*.h'
s.frameworks = 'UIKit', 'MapKit'
s.dependency 'AFNetworking', '~> 2.3'
s.dependency 'CocoaLumberjack', '~> 2.2.0'
s.dependency 'MBProgressHUD', '~> 0.9.2'
s.dependency 'Masonry', '~> 0.6.4'
s.dependency 'ReactiveCocoa', '~> 2.5'
s.dependency 'YapDatabase', '~> 2.8.3'
s.dependency 'DTCoreText', '~> 1.6.17'
s.dependency 'MJExtension', '~> 3.0.10'
end

创建完毕之后,随手使用
pod lib lint
命令验证是否编写正确,如果没有正确,根据提示修改即可.

几个好用的命令:

完整打印详情,可以帮助我们调试,用法 ` pod lib lint --verbose`
1
 
 
 ```--allow-warnings``` 忽略警告,由于一些三方代码有警告,所以造成始终无法通过,可以用这个方法忽略警告,用法同上
 


### 创建代码仓库
 在github 或者其他托管平台建立一个空项目,最好项目名称和本地的一致

```objc

git add .
git commit -m “Initial Commit"
git remote add origin https://github.com/kennyGui/GUICodeSnippet.git 
git push -u origin master

其实我更喜欢的方式是, Clone 空项目,然后替换代码的 .git 文件夹,这样更暴力,具体怎么选择,看个人喜好.

打 tag

1
2
3
4
5
6
7
8
git tag -a 0.1.0 -m "初始版本"

通常的git push不会将标签对象提交到git服务器,我们需要进行显式的操作:
git push origin 0.1.2 # 将v0.1.2标签提交到git服务器
或者
git push origin –tags # 将本地所有标签一次性提交到git服务器
验证标签成功
git tag #查看标签

推送描述文件

使用这个命令推送自己的库的信息到官方的 Master 库

trunk push GUICodeSnippet.podspec ```objc
1

但是如果你是第一次使用的话,需要注册 Session
使用如下命令注册自己的邮箱(注意替换成自己的邮箱)

pod trunk register orta@cocoapods.org ‘Orta Therox’ —description=’macbook air’

1
2
3
稍后, 邮箱会收到邮件, 点击链接即可

``` pod trunk push BlinkingLabel.podspec

由于网络原因(你懂得),可能会推送失败,多尝试几次即可.

验证

可以让同事或者朋友使用

1
pod search GUICodeSnippet

这个命令进行测试,但是,如果是第一次,应该是找不到的.
需要使用
pod setup更新一下本地的 Maser 库信息

结语

好了,现在,你就可以将自己的代码共享给小伙伴使用啦.

现在 pod 里面的 Class 都是放在一个文件夹下的,没有办法实现子文件夹,(即使工程里面有多个文件夹,但是弄好 cocoapods 之后,也是都放到一个文件夹下面了)

这需要我们的 spec 里面配置 subspec ,这个知识点会在下一篇文章中说