0%

制作CocoaPods公开库

1. 创建要公开的pod库

主要包含pod源码和pod sepc文件、许可证等。

这个过程可以先在本地创建好,再到GitHub创建对应的仓库,然后将本地pod添加到远程仓库。

也可以先在GitHub创建好仓库,再clone到本地,然后在clone的目录下创建pod。

pod sepc文件的说明可以参考:

CocoaPods 系列之三 Podspec 语法说明

cocoapods官网文档

Podspec Syntax Reference

创建sepc文件:

1
pod spec create 库名

示例sepc文件如下:

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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
#
# Be sure to run `pod spec lint Peanut.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 https://guides.cocoapods.org/syntax/podspec.html
# To see working Podspecs in the CocoaPods repo see https://github.com/CocoaPods/Specs/
#

Pod::Spec.new do |spec|

# ――― 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.
#

spec.name = "Peanut"
spec.version = "0.0.1"
spec.summary = "A short description of Peanut."
spec.homepage = "http://EXAMPLE/Peanut"


# ――― Spec License ――――――――――――――――――――――――――――――――――――――――――――――――――――――――――― #
#
# Licensing your code is important. See https://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'.
#

spec.license = { :type => "MIT", :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.
#

spec.author = { "mt" => "邮箱" }


# ――― 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.
#

spec.platform = :ios, "10.0"


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

spec.source = { :git => "http://EXAMPLE/Peanut.git", :tag => "#{spec.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.
#

spec.source_files = "Peanut/Classes/**/*"
# spec.exclude_files = "Classes/Exclude"
# spec.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.
#

spec.resources = "Resources/*"
# spec.preserve_paths = "FilesToSave", "MoreFilesToSave"


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

spec.frameworks = "Foundation", "UIKit"
# spec.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.

spec.requires_arc = true

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

end

看上面的注释很容易看懂。

2. 验证pod库

pod源码和pod sepc文件都准备好后,别着急发布,先验证pod库的可用性:

pod spec lint xxx.podsepc

验证库坑:

pod spec lint name.podsepc

一直提示:CDN: trunk URL couldn’t be downloaded

1
2
-> XQSheet (2.0.0)
- ERROR | [iOS] unknown: Encountered an unknown error (CDN: trunk URL couldn't be downloaded: https://raw.githubusercontent.com/CocoaPods/Specs/master/Specs/4/2/1/JKPresentationController/1.0.0/JKPresentationController.podspec.json, error: Failed to open TCP connection to raw.githubusercontent.com:443 (Connection refused - connect(2) for "raw.githubusercontent.com" port 443)) during validation.

解决办法:指定source。

pod spec lint name.podsepc --sources='https://github.com/CocoaPods/Specs.git'

允许警告可以添加:—allow-warnings

1
pod spec lint GSDMediaCache.podspec --allow-warnings

如果使用pod还依赖了第三方库则还需要添加—use-libraries:表示使用静态库或者是framework,这里主要是解决当我们依赖一些framework库后校验提示找不到库的时候用到。

eg:

1
pod spec lint GSDMediaCache.podspec --allow-warnings --use-libraries

3.提交pod到GitHub,并打tag

这个就是平时开发提交代码,打tag。

前面这几步和制作私有库是一样的都是准备工作。

4. 发布

要想发布一个公开库,需要先注册一个 CocoaPods 账号。

查看有没有注册过:

pod trunk me

如果没有那么先注册:

pod trunk register EMAIL [NAME]

eg:

pod trunk register orta@cocoapods.org 'Orta Therox' --description='macbook air'

注册好后,开始发布,使用命令:

pod trunk push xxx.podspec

将pod spec文件推送到cocoapods的官方仓库,这样别人才能找得到。

注意:pod trunk push也会验证库,所以上面预验证的参数还得加:

1
pod trunk push GSDMediaCache.podspec --allow-warnings --use-libraries

推送库坑:

pod trunk push XQSheet.podspec

1
2
3
4
5
6
7
8
9
10
11
12
Updating spec repo `trunk`

CocoaPods 1.9.1 is available.
To update use: `sudo gem install cocoapods`

For more information, see https://blog.cocoapods.org and the CHANGELOG for this version at https://github.com/CocoaPods/CocoaPods/releases/tag/1.9.1

Validating podspec
-> XQSheet (2.0.0)
- ERROR | [iOS] unknown: Encountered an unknown error (CDN: trunk URL couldn't be downloaded: https://raw.githubusercontent.com/CocoaPods/Specs/master/Specs/4/2/1/JKPresentationController/1.0.0/JKPresentationController.podspec.json, error: Failed to open TCP connection to raw.githubusercontent.com:443 (Connection refused - connect(2) for "raw.githubusercontent.com" port 443)) during validation.

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

一直卡在这里,最后让终端翻墙后好了.

5. 验证结果

推送到CocoaPods成功后,我们可以搜索刚才制作的pod。此时一般是搜不到的,因为你本地的搜索索引文件,pod spec仓库都是旧的。

发布成功后搜索不到解决办法:

1
2
3
4
5
6
7
8
//更新spec仓库。更新后尝试搜索,如果还搜不到就继续后面一步。
pod repo update

//删除本地索引
rm ~/Library/Caches/CocoaPods/search_index.json

//搜索
pod search [库名]

扩展

上述步骤每次操作也都挺繁琐的,可以考虑自动化,参考:一行命令发布 Pod 框架

其他

上述pod是托管到某个平台上的,如果不想上传到服务器,也可以托管在本地玩玩。

安装本地pod

假设已经有了一个本地pod。JKPresentationController.podspec如下:

1
2
spec.source       = { :git => "./JKPresentationController/", :tag => "#{spec.version}" }
spec.source_files = "JKPresentationController/*.swift"

git后面是源码本地路径.

Podfile文件:

1
2
3
4
5
6
7
8
9
10
11
source 'https://github.com/CocoaPods/Specs.git'

platform :ios, '10.0'
use_frameworks!

target 'JKPresentationControllerDemo' do

pod 'JKPresentationController', :path => '../'
pod 'SnapKit', '~> 5.0.0'

end

path后面是JKPresentationController.podspec所在的路径.

执行pod install后,Pods工程下会出现一个Development Pods目录.该目录中包含你的源码文件夹.

相关阅读:制作CocoaPods私有库

觉得文章有帮助可以打赏一下哦!