手把手教你发布代码到CocoaPods

目录

  • 前言
  • CocoaPods原理
  • 发布与更新自己的pod
    • 安装CocoaPods
    • 创建编辑自己的pod
    • 本地初始化pod并提交到Github
    • 注册CocoaPods并提交pod
    • 更新自己的pod
    • 其他命令

前言

最近App在整理一些第三方库,包括一些没怎么再维护的替换为自己实现,所以做了一些简单的库上传到CocoaPods方便管理和维护。

CocoaPods原理

每种语言发展到一定阶段,就会出现相应的依赖管理工具,例如Node.js的npm、Swift的Swift Package Manager,早期为iOS提供依赖管理的工具叫做:CocoaPods。

特点:

  • CocoaPods会将所有的第三方库以target的方式组成一个名为Pods的工程。整个第三方库工程会生成一个库提供给我们使用。
  • 我们的工程和Pods工程会被以workspace的形式组织和管理,也就是*.xcworkspace文件。

好处:

  • CocoaPods上有很多好用的库,以此缩短我们的开发周期和提升软件的质量。
  • CocoaPods会自动将第三方开源库的源码下载下来,并且为工程设置好相应的系统依赖和编译参数。
  • 通过Podfile可以很方便地更新第三方库。

发布与更新自己的pod

下面以我提交的库ZInAppPurchase为例,讲解下整个过程和遇到的问题。

参考官方文档Making CocoaPods

安装CocoaPods

CocoaPods是用Ruby实现的,而Gem是一个管理Ruby库和程序的标准包,它通过Ruby Gem源来查找、安装、升级和卸载软件包。

  • 更新Gem

如果没有翻墙可能会比较慢,可以将原来的源替换成新的。

// 查看本机当前设置的源
ZackZhengdeMacBook-Pro:~ zack$ gem source -l
*** CURRENT SOURCES ***

https://rubygems.org/
// 移除掉原有的源
ZackZhengdeMacBook-Pro:~ zack$ gem sources --remove https://rubygems.org/
// 添加国内的源
ZackZhengdeMacBook-Pro:~ zack$ gem sources -a https://ruby.taobao.org/

淘宝的源已不再维护,可以使用新的源,参考RubyGems 镜像 - Ruby China

接下来更新Gem:

ZackZhengdeMacBook-Pro:~ zack$ sudo gem update --system
  • 安装并初始化CocoaPods
ZackZhengdeMacBook-Pro:~ zack$ sudo gem install cocoapods --pre
Password:
Successfully installed cocoapods-1.1.0.rc.3
Parsing documentation for cocoapods-1.1.0.rc.3
Done installing documentation for cocoapods after 2 seconds
1 gem installed

使用 --pre 是为了尽量使用最新的版本,没更新时遇到过pod lib lint发生下面错误的情况:

xcodebuild: error: 'App.xcworkspace' does not exist.

初始化:

ZackZhengdeMacBook-Pro:~ zack$ pod setup
Setting up CocoaPods master repo
  $ /usr/bin/git -C /Users/zack/.cocoapods/repos/master pull --ff-only
  From https://github.com/CocoaPods/Specs
     cae692c..3231da7  master     -> origin/master
  Updating cae692c..3231da7
  Fast-forward
  ......
Setup completed

所有项目的Podspec文件都托管在 https://github.com/CocoaPods/Specs 第一次执行 pod setup时,CocoaPods会将这些podspec索引文件更新到本地的 ~/.cocoapods/目录下,这个索引文件比较大。网上有讲解更换repo镜像的方法。

创建编辑自己的pod

使用pod lib的命令。

ZackZhengdeMacBook-Pro:~ zack$ pod lib create ZInAppPurchase
Cloning `https://github.com/CocoaPods/pod-template.git` into `ZInAppPurchase`.
Configuring ZInAppPurchase template.

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

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: 
 - http://guides.cocoapods.org/making/using-pod-lib-create.html
 ( hold cmd and double click links to open in a browser. )


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

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

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

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

Running pod install on your new library.

Analyzing dependencies
Fetching podspec for `ZInAppPurchase` from `../`
Downloading dependencies
Installing ZInAppPurchase (0.1.0)
Generating Pods project
Integrating client project

[!] Please close any current Xcode sessions and use `ZInAppPurchase.xcworkspace` for this project from now on.
Sending stats
Pod installation complete! There is 1 dependency from the Podfile and 1 total pod installed.

 Ace! you're ready to go!
 We will start you off by opening your project in Xcode
  open 'ZInAppPurchase/Example/ZInAppPurchase.xcworkspace'

To learn more about the template see `https://github.com/CocoaPods/pod-template.git`.
To learn more about creating a new pod, see `http://guides.cocoapods.org/making/making-a-cocoapod`.

修改文件:

/Users/zack/ZInAppPurchase/ZInAppPurchase/Classes/ReplaceMe.swift
/Users/zack/ZInAppPurchase/ZInAppPurchase.podspec

.podspec文件主要是修改s.homepages.sources.frameworks*。

ZInAppPurchase.podspec内容如下:

Pod::Spec.new do |s|
  s.name             = 'ZInAppPurchase'
  s.version          = '0.1.1'
  s.summary          = 'A short description of ZInAppPurchase.'

  s.description      = <<-DESC
TODO: Add long description of the pod here.
                       DESC

  s.homepage         = 'https://github.com/sapphirezzz/ZInAppPurchase'
  s.license          = { :type => 'MIT', :file => 'LICENSE' }
  s.author           = { 'sapphirezzz' => 'zhengzuanzhe@gmail.com' }
  s.source           = { :git => 'https://github.com/sapphirezzz/ZInAppPurchase.git', :tag => s.version.to_s }

  s.ios.deployment_target = '8.0'

  s.source_files = 'ZInAppPurchase/Classes/**/*'
  s.frameworks = 'Foundation', 'StoreKit'
end

安装pod:

ZackZhengdeMacBook-Pro:~ zack$ cd ZInAppPurchase/Example/
ZackZhengdeMacBook-Pro:Example zack$ pod install
Analyzing dependencies
Fetching podspec for `ZInAppPurchase` from `../`
Downloading dependencies
Installing ZInAppPurchase 0.1.0 (was 0.1.0)
Generating Pods project
Integrating client project
Sending stats
Pod installation complete! There is 1 dependency from the Podfile and 1 total pod installed.

此时会看到工程目录下多出ZInAppPurchase.xcworkspace、Podfile.lock文件和Pods目录。

接下来可以打开下面文件编写库:

/Users/zack/ZInAppPurchase/Example/ZInAppPurchase.xcworkspace

本地初始化pod并提交到Github

先到Github建立仓库ZInAppPurchase,并提交代码:

ZackZhengdeMacBook-Pro:ZInAppPurchase zack$ git add -A && git commit -m "Release 0.1.0"
ZackZhengdeMacBook-Pro:ZInAppPurchase zack$ git tag '0.1.1'
ZackZhengdeMacBook-Pro:ZInAppPurchase zack$ git remote add origin git@github.com:sapphirezzz/ZInAppPurchase.git
ZackZhengdeMacBook-Pro:ZInAppPurchase zack$ git push --tags
ZackZhengdeMacBook-Pro:ZInAppPurchase zack$ git push origin master

初始化pod:

ZackZhengdeMacBook-Pro:ZInAppPurchase zack$ pod lib lint --allow-warnings

 -> ZInAppPurchase (0.1.0)
    - WARN  | summary: The summary is not meaningful.

ZInAppPurchase passed validation.

使用--allow-warnings是使得有warning时也能验证通过。比如:WARN | summary: The summary is not meaningful.

注册CocoaPods并提交pod

先注册:

ZackZhengdeMacBook-Pro:ZInAppPurchase zack$ pod trunk register zhengzuanzhe@gmail.com 'zackzheng' --description='macbook pro'
[!] Please verify the session by clicking the link in the verification email that has been sent to zhengzuanzhe@gmail.com

然后打开收到的标题为[CocoaPods] Confirm your session.的邮件,点击链接之后跳转页面,会看到:

You can go back to your terminal now.

最后就deploy了:

ZackZhengdeMacBook-Pro:ZInAppPurchase zack$ pod trunk push ZInAppPurchase.podspec --allow-warnings
Updating spec repo `master`
Validating podspec
 -> ZInAppPurchase (0.1.0)
    - WARN  | summary: The summary is not meaningful.

Updating spec repo `master`
  - Data URL: https://raw.githubusercontent.com/CocoaPods/Specs/ae961ca47afb2a9171653382214897c4e160a679/Specs/ZInAppPurchase/0.1.0/ZInAppPurchase.podspec.json
  - Log messages:
    - October 13th, 11:28: Push for `ZInAppPurchase 0.1.0' initiated.
    - October 13th, 11:28: Push for `ZInAppPurchase 0.1.0' has been pushed
    (1.423909231 s).

看看库是否已经可以找到:

ZackZhengdeMacBook-Pro:ZInAppPurchase zack$ pod search ZInAppPurchase
-> ZInAppPurchase (0.1.0)
   A short description of ZInAppPurchase.
   pod 'ZInAppPurchase', '~> 0.1.0'
   - Homepage: https://github.com/sapphirezzz/ZInAppPurchase
   - Source:   https://github.com/sapphirezzz/ZInAppPurchase.git
   - Versions: 0.1.0 [master repo]
(END)

组合键shift+Q退出

更新自己的pod

修改完代码之后,提交到Github并push到CocoaPods:

ZackZhengdeMacBook-Pro:ZInAppPurchase zack$ git add -A && git commit -m 
ZackZhengdeMacBook-Pro:ZInAppPurchase zack$ git tag '0.1.1'
ZackZhengdeMacBook-Pro:ZInAppPurchase zack$ git push --tags
ZackZhengdeMacBook-Pro:ZInAppPurchase zack$ pod trunk push ZInAppPurchase.podspec --allow-warnings
Updating spec repo `master`
Validating podspec
 -> ZInAppPurchase (0.1.1)
    - WARN  | summary: The summary is not meaningful.

Updating spec repo `master`
  - Data URL: https://raw.githubusercontent.com/CocoaPods/Specs/1fba9c650a4212c0b7be707698b8597556f0ece7/Specs/ZInAppPurchase/0.1.1/ZInAppPurchase.podspec.json
  - Log messages:
    - October 13th, 12:42: Push for `ZInAppPurchase 0.1.1' initiated.
    - October 13th, 12:43: Push for `ZInAppPurchase 0.1.1' has been pushed
    (1.090057228 s).

其他命令

查看CocoaPods上的个人信息:

ZackZhengdeMacBook-Pro:ZInAppPurchase zack$ pod trunk me
  - Name:     Zackzheng
  - Email:    zhengzuanzhe@gmail.com
  - Since:    May 30th, 21:28
  - Pods:
    - ZInAppPurchase
    - ZPlaceholderTextView
    - ZPopTipView
  - Sessions:
    - May 30th, 21:28       -        October 6th, 00:10. IP: 14.18.48.167 
    
    - September 28th, 02:06 - February 18th, 2017 10:43. IP: 121.33.185.113
    Description: macbook pro
    - October 13th, 11:24   - February 18th, 2017 11:31. IP: 183.240.19.242
    Description: macbook pro

有时候如果提交有问题,可以执行一下命令删除:

// 直接废去这个pod

pod trunk deprecate ZInAppPurchase

// 废去这个pod的某个版本

pod trunk delete ZInAppPurchase 1.0.0

测试过,两个命令使用完都没返回成功,且pod search是可以找到的,但实际却是成功的。

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 203,324评论 5 476
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 85,303评论 2 381
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 150,192评论 0 337
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 54,555评论 1 273
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 63,569评论 5 365
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 48,566评论 1 281
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 37,927评论 3 395
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 36,583评论 0 257
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 40,827评论 1 297
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 35,590评论 2 320
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 37,669评论 1 329
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 33,365评论 4 318
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 38,941评论 3 307
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 29,928评论 0 19
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 31,159评论 1 259
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 42,880评论 2 349
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 42,399评论 2 342

推荐阅读更多精彩内容