利用Cocoapods制作公开库

在本文开始之前,强烈建议先在CocoaPods官网上学习,仍然有疑问的,再来看本文的教程。

本文采用Trunk方式来制作公开库。
1、打开终端,在自己喜欢的目录下,执行pod lib create [pod name]。下图我创建的公开库的名称是YunxiaoLibrary.

wenjun@WendeMacBook-Pro MyPublicLib % pod lib create YunxiaoLibrary
Cloning `https://github.com/CocoaPods/pod-template.git` into `YunxiaoLibrary`.
Configuring YunxiaoLibrary template.
security: SecKeychainSearchCopyNext: The specified item could not be found in the keychain.

------------------------------

To get you started we need to ask a few questions, this should only take a minute.

If this is your first time we recommend running through with the guide: 
 - https://guides.cocoapods.org/making/using-pod-lib-create.html
 ( hold cmd and double click links to open in a browser. )

2、接着根据实际情况,回答几个问题

To get you started we need to ask a few questions, this should only take a minute.

If this is your first time we recommend running through with the guide: 
 - https://guides.cocoapods.org/making/using-pod-lib-create.html
 ( hold cmd and double click links to open in a browser. )


What platform do you want to use?? [ iOS / macOS ]
 > iOS

What language do you want to use?? [ Swift / ObjC ]
 > ObjC

Would you like to include a demo application with your library? [ Yes / No ]
 > Yes

Which testing frameworks will you use? [ Specta / Kiwi / None ]
 > None

Would you like to do view based testing? [ Yes / No ]
 > No

What is your class prefix?
 > YX

3、第2步结束后,会自定打开Xcode目录
目录结构和各文件的说明,请参考using-pod-lib-create

4、在Trunk上注册自己的账号(用邮箱注册)

wenjun@WendeMacBook-Pro MyPublicLib % pod trunk register xxx@163.com '你的用户名' --description='macbook pro' 

注册后,打开上述邮箱中的邮件,点击激活链接去激活

5、pod trunk me查看注册信息

wenjun@WendeMacBook-Pro MyPublicLib % pod trunk me
  - Name:     XXX
  - Email:    xxx@163.com
  - Since:    August 24th, 2018 03:49
  - Pods:
    - XXX
  - Sessions:
    - April 28th, 20:00 - September 3rd, 20:03. IP: 18.162.242.196
    Description: macbook pro

因为我的邮箱之前注册过,所以Pods下有一个库了。

6、在Github上新建一个公开仓库,用来存放该公开库
因为后面要修改[NAME.podspec]文件,该文件中的要指定公开库地址,所以在这一步把仓库建好。

新建公开仓库

7、将本地公开库,推送到刚才新建的远程仓库

wenjun@WendeMacBook-Pro MyPublicLib % cd /Users/wenjun/Workspace/MyPublicLib/YunxiaoLibrary
wenjun@WendeMacBook-Pro YunxiaoLibrary % ls
Example         README.md       YunxiaoLibrary.podspec
LICENSE         YunxiaoLibrary      _Pods.xcodeproj
wenjun@WendeMacBook-Pro YunxiaoLibrary % git remote add origin https://github.com/WJLollipop/YunxiaoLibrary.git
wenjun@WendeMacBook-Pro YunxiaoLibrary % git branch -M master
wenjun@WendeMacBook-Pro YunxiaoLibrary % git push -u origin master

8、打Tag,将Tag推送到远程
这一步需要注意的是,Tag号要跟podspec中的version保持一致,这里的版本号是0.1.1.

wenjun@WendeMacBook-Pro YunxiaoLibrary % git tag -a 0.1.1 -m "version 0.1.1"
wenjun@WendeMacBook-Pro YunxiaoLibrary % git push origin --tags
Enumerating objects: 1, done.
Counting objects: 100% (1/1), done.
Writing objects: 100% (1/1), 171 bytes | 171.00 KiB/s, done.
Total 1 (delta 0), reused 0 (delta 0)
To https://github.com/WJLollipop/YunxiaoLibrary.git
 * [new tag]         0.1.1 -> 0.1.1

9、修改podspec文件
上面的仓库建好了,tag也有了,接下来就是在Xcode中编辑podspec文件。
各参数的配置,可以参考各字段说明
这里提一下几个点:

  • 参数后面跟着 R 标记的,代表是Required,是必填的,不然接下去的验证会通不过。
  • s.version要和tag版本一致
  • s.source就是新建的仓库地址
  • s.public_header_files是公开的头文件
  • s.frameworks是需要依赖的系统库
  • s.dependency依赖的三方库

我的配置如下,供大家参考:

#
# Be sure to run `pod lib lint YunxiaoLibrary.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 https://guides.cocoapods.org/syntax/podspec.html
#

Pod::Spec.new do |s|
  s.name             = 'YunxiaoLibrary'
  s.version          = '0.1.1'
  s.summary          = 'This is a cornner and has a default backgroundColor button.'

# 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: Help developers to creat a cornner button.
                       DESC

  s.homepage         = 'https://github.com/WJLollipop/YunxiaoLibrary'
  # s.screenshots     = 'www.example.com/screenshots_1', 'www.example.com/screenshots_2'
  s.license          = { :type => 'MIT', :file => 'LICENSE' }
  s.author           = { 'Frank' => 'yunxiao.wen@tuya.com' }
  s.source           = { :git => 'https://github.com/WJLollipop/YunxiaoLibrary.git', :tag => s.version.to_s }
  # s.social_media_url = 'https://twitter.com/<TWITTER_USERNAME>'

  s.ios.deployment_target = '9.0'

  s.source_files = 'YunxiaoLibrary/Classes/**/*'
  
  # s.resource_bundles = {
  #   'YunxiaoLibrary' => ['YunxiaoLibrary/Assets/*.png']
  # }

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

10、本地验证
在终端输入pod spec lint [NAME.podspec]。如果有警告,在命令后面加上--allow-warnings

wenjun@WendeMacBook-Pro YunxiaoLibrary % pod lib lint YunxiaoLibrary.podspec

 -> YunxiaoLibrary (0.1.1)
    - WARN  | description: The description is shorter than the summary.
    - NOTE  | xcodebuild:  note: Using new build system
    - NOTE  | xcodebuild:  note: Building targets in parallel
    - NOTE  | xcodebuild:  note: Using codesigning identity override: -
    - NOTE  | [iOS] xcodebuild:  note: Planning build
    - NOTE  | [iOS] xcodebuild:  note: Constructing build description
    - NOTE  | [iOS] xcodebuild:  warning: Skipping code signing because the target does not have an Info.plist file and one is not being generated automatically. (in target 'App' from project 'App')

[!] YunxiaoLibrary did not pass validation, due to 1 warning (but you can use `--allow-warnings` to ignore it).
You can use the `--no-clean` option to inspect any issue.
wenjun@WendeMacBook-Pro YunxiaoLibrary % pod lib lint YunxiaoLibrary.podspec --allow-warnings

 -> YunxiaoLibrary (0.1.1)
    - WARN  | description: The description is shorter than the summary.
    - NOTE  | xcodebuild:  note: Using new build system
    - NOTE  | xcodebuild:  note: Building targets in parallel
    - NOTE  | xcodebuild:  note: Using codesigning identity override: -
    - NOTE  | [iOS] xcodebuild:  note: Planning build
    - NOTE  | [iOS] xcodebuild:  note: Constructing build description
    - NOTE  | [iOS] xcodebuild:  warning: Skipping code signing because the target does not have an Info.plist file and one is not being generated automatically. (in target 'App' from project 'App')

YunxiaoLibrary passed validation.

11、回到Xcode,进行Pod Install

wenjun@WendeMacBook-Pro Example % pod install
Analyzing dependencies
Downloading dependencies
Installing YunxiaoLibrary 0.1.1 (was 0.1.0)
Generating Pods project
Integrating client project
Pod installation complete! There is 1 dependency from the Podfile and 1 total pod installed.

可见,本地YunxiaoLibrary 0.1.1已经更新

12、Push 到 Trunk
先切换到podspec目录下,执行pod trunk push YunxiaoLibrary.podspec。如果报警告没通过,在命令后加上--allow-warnings

wenjun@WendeMacBook-Pro YunxiaoLibrary % pod trunk push YunxiaoLibrary.podspec                
Updating spec repo `trunk`
Validating podspec
 -> YunxiaoLibrary (0.1.1)
    - WARN  | description: The description is shorter than the summary.
    - NOTE  | xcodebuild:  note: Using new build system
    - NOTE  | xcodebuild:  note: Building targets in parallel
    - NOTE  | xcodebuild:  note: Using codesigning identity override: -
    - NOTE  | [iOS] xcodebuild:  note: Planning build
    - NOTE  | [iOS] xcodebuild:  note: Constructing build description
    - NOTE  | [iOS] xcodebuild:  warning: Skipping code signing because the target does not have an Info.plist file and one is not being generated automatically. (in target 'App' from project 'App')

[!] The spec did not pass validation, due to 1 warning (but you can use `--allow-warnings` to ignore it).
wenjun@WendeMacBook-Pro YunxiaoLibrary % pod trunk push YunxiaoLibrary.podspec --allow-warnings
Updating spec repo `trunk`
Validating podspec
 -> YunxiaoLibrary (0.1.1)
    - WARN  | description: The description is shorter than the summary.
    - NOTE  | xcodebuild:  note: Using new build system
    - NOTE  | xcodebuild:  note: Building targets in parallel
    - NOTE  | xcodebuild:  note: Using codesigning identity override: -
    - NOTE  | [iOS] xcodebuild:  note: Planning build
    - NOTE  | [iOS] xcodebuild:  note: Constructing build description
    - NOTE  | [iOS] xcodebuild:  warning: Skipping code signing because the target does not have an Info.plist file and one is not being generated automatically. (in target 'App' from project 'App')

Updating spec repo `trunk`

--------------------------------------------------------------------------------
 🎉  Congrats

 🚀  YunxiaoLibrary (0.1.1) successfully published
 📅  April 28th, 22:00
 🌎  https://cocoapods.org/pods/YunxiaoLibrary
 👍  Tell your friends!
--------------------------------------------------------------------------------
wenjun@WendeMacBook-Pro YunxiaoLibrary % 

到这,恭喜你创建公开库成功!

13、验证
搜索下公开库,看能不能搜到。

wenjun@WendeMacBook-Pro YunxiaoLibrary % pod search YunxiaoLibrary
[!] Unable to find a pod with name, author, summary, or description matching `YunxiaoLibrary`

如果报错,请打开: ~/Library/Caches/CocoaPods ,把search_index.json 文件删除再重新 pod search

wenjun@WendeMacBook-Pro YunxiaoLibrary % pod search YunxiaoLibrary
[!] Unable to find a pod with name, author, summary, or description matching `YunxiaoLibrary`
wenjun@WendeMacBook-Pro YunxiaoLibrary % pod search YunxiaoLibrary
Creating search index for spec repo 'cocoapods'.. Done!
Creating search index for spec repo 'tuya-inc-tuyaios-tyspecs'.. Done!
Creating search index for spec repo 'tuya-inc-tuyaios-tyspecsdebug'.. Done!
Creating search index for spec repo 'tuya-inc-tuyaios-tyspecsrelease'.. Done!
Creating search index for spec repo 'tuya-inc-tuyaios-tyspecsthird'.. Done!
Creating search index for spec repo 'tuyainc'.. Done!
wenjun@WendeMacBook-Pro YunxiaoLibrary % pod search YunxiaoLibrary
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 160,504评论 4 365
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 67,898评论 1 300
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 110,218评论 0 248
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 44,322评论 0 214
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 52,693评论 3 290
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 40,812评论 1 223
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 32,010评论 2 315
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 30,747评论 0 204
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 34,476评论 1 246
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 30,700评论 2 251
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 32,190评论 1 262
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 28,541评论 3 258
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 33,206评论 3 240
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 26,129评论 0 8
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,903评论 0 199
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 35,894评论 2 283
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 35,748评论 2 274

推荐阅读更多精彩内容