制作、发布自己的第三方代码库

很多时候,自己写了一个小工具,希望在自己的每个app上都能够灵活的使用,这个时候选择用CocoaPod来管理要方便的多,所以我们来学习一下如何制作自己的第三方代码库。

制作本地库

在自己本地新建一个文件夹Demo,然后再控制台中cd到该目录下,输入指令创建库,ComAlertDialogTool是我们要创建的库的名字

pod lib create ComAlertDialogTool

输入命令后会显示下载模板,会有几秒钟等待,这里有几项需要我们输入信息

chenyuandeMacBook-Pro:~ chenyuan$ pod lib create ComAlertDialogTool
Cloning `https://github.com/CocoaPods/pod-template.git` into `ComAlertDialogTool`.
Configuring ComAlertDialogTool 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: 
 - 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)
 > None

Would you like to do view based testing? [ Yes / No ]     //是否要UI测试?
 > Yes

What is your class prefix?     //类名前缀是什么?
 > Demo

Running pod install on your new library.

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

[!] Please close any current Xcode sessions and use `ComAlertDialogTool.xcworkspace` for this project from now on.
Sending stats
Pod installation complete! There are 2 dependencies from the Podfile and 2 total pods installed.

[!] Automatically assigning platform `ios` with version `9.3` on target `ComAlertDialogTool_Example` because no platform was specified. Please specify a platform for this target in your Podfile. See `https://guides.cocoapods.org/syntax/podfile.html#platform`.

 Ace! you're ready to go!
 We will start you off by opening your project in Xcode
  open 'ComAlertDialogTool/Example/ComAlertDialogTool.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`.
chenyuandeMacBook-Pro:~ chenyuan$ 

然后我们就创建好了一个项目,看他的文件结构如下:
我们可以在ReplaceMe.m位置添加自己的代码。
注意:听说这个里面最多只能有一层文件夹。


屏幕快照 2018-04-25 下午5.49.28.png

3DC86090-1BA5-450A-A71F-60CBDF02A434.png

添加完代码后,记得查看Podfile文件,并执行pod install
到这里本地库我们已经制作好了,如果把我们本地当做git库的话,这时候我们只需要在你其他的项目或者工程的Podfile文件里添加ComAlertDialogTool:

use_frameworks!

target 'ComAlertDialogTool_Example' do
  pod 'ComAlertDialogTool', :path => '../'             #路径根据实际情况而定
  
end
本地库提交到CocoaPods远程仓库

我们使用CocoaPods的指令创建好本地库之后,默认生成的podspec文件,它描述该库某一个版本的信息。

#
# Be sure to run `pod lib lint ComAlertDialogTool.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             = 'ComAlertDialogTool'
  s.version          = '0.0.1'       //版本,每次发布pod库都要修改
  s.summary          = 'A short description of ComAlertDialogTool.'

# 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/ychen3022/ComAlertDialogTool'
  # s.screenshots     = 'www.example.com/screenshots_1', 'www.example.com/screenshots_2'
  s.license          = { :type => 'MIT', :file => 'LICENSE' }
  s.author           = { 'ychen3022' => '3023056944@qq.com' }
  s.source           = { :git => 'https://github.com/ychen3022/ComAlertDialogTool.git', :tag => s.version.to_s }
  # s.social_media_url = 'https://twitter.com/<TWITTER_USERNAME>'

  s.ios.deployment_target = '8.0'

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

  # s.public_header_files = 'Pod/Classes/**/*.h'
  # s.frameworks = 'UIKit', 'MapKit'
    s.dependency 'ComTools', '~> 0.0.3'            //依赖的其他pod库

end

在确定自己的.podspec没有问题之后,我们需要将代码上传到git上面
首先在自己的github仓库上创建一个工程


屏幕快照 2018-05-02 下午1.40.20.png

然后执行下面的git命令将代码上传到github上面

git init
git remote add origin https://github.com/ychen3022/ComAlertDialogTool
git add .
git commit -m "ComAlertDialogTool-firstCommit"
git push origin master
git tag 0.0.1
git push origin 0.0.1

成功将代码传到git上后,我们再提交到官方的CocoaPods
首先先用cd到Spec文件所在的目录,然后执行pod lib lint / pod lib lint --allow-warnings
当出现ComAlertDialogTool passed validation.的字样,说明通过验证。

chenyuandeMacBook-Pro:ComAlertDialogTool chenyuan$ ls       //需要在.podspec文件所在目录下面
ComAlertDialogTool      Example             README.md
ComAlertDialogTool.podspec  LICENSE             _Pods.xcodeproj
chenyuandeMacBook-Pro:ComAlertDialogTool chenyuan$ pod lib lint

 -> ComAlertDialogTool (0.0.1)
    - WARN  | summary: The summary is not meaningful.
    - WARN  | xcodebuild:  /Users/chenyuan/ComAlertDialogTool/ComAlertDialogTool/Classes/ComAlertView.m:205:10: warning: block implicitly retains 'self'; explicitly mention 'self' to indicate this is intended behavior [-Wimplicit-retain-self]
    - WARN  | xcodebuild:  /Users/chenyuan/ComAlertDialogTool/ComAlertDialogTool/Classes/ComAlertView.m:243:9: warning: block implicitly retains 'self'; explicitly mention 'self' to indicate this is intended behavior [-Wimplicit-retain-self]
    - WARN  | xcodebuild:  /Users/chenyuan/ComAlertDialogTool/ComAlertDialogTool/Classes/ComAlertView.m:276:9: warning: block implicitly retains 'self'; explicitly mention 'self' to indicate this is intended behavior [-Wimplicit-retain-self]
    - WARN  | xcodebuild:  /Users/chenyuan/ComAlertDialogTool/ComAlertDialogTool/Classes/ComAlertView.m:277:9: warning: block implicitly retains 'self'; explicitly mention 'self' to indicate this is intended behavior [-Wimplicit-retain-self]
    - WARN  | xcodebuild:  /Users/chenyuan/ComAlertDialogTool/ComAlertDialogTool/Classes/ComLoadingView.m:142:13: warning: block implicitly retains 'self'; explicitly mention 'self' to indicate this is intended behavior [-Wimplicit-retain-self]
    - WARN  | xcodebuild:  /Users/chenyuan/ComAlertDialogTool/ComAlertDialogTool/Classes/ComLoadingView.m:306:20: warning: incompatible pointer types returning 'UIView *' from a function with result type 'ComLoadingView *' [-Wincompatible-pointer-types]
    - WARN  | xcodebuild:  /Users/chenyuan/ComAlertDialogTool/ComAlertDialogTool/Classes/ComLoadingView.m:317:20: warning: incompatible pointer types returning 'UIView *' from a function with result type 'ComLoadingView *' [-Wincompatible-pointer-types]
    - WARN  | xcodebuild:  /Users/chenyuan/ComAlertDialogTool/ComAlertDialogTool/Classes/ComToastView.m:116:9: warning: block implicitly retains 'self'; explicitly mention 'self' to indicate this is intended behavior [-Wimplicit-retain-self]
    - WARN  | xcodebuild:  /Users/chenyuan/ComAlertDialogTool/ComAlertDialogTool/Classes/ComToastView.m:147:9: warning: block implicitly retains 'self'; explicitly mention 'self' to indicate this is intended behavior [-Wimplicit-retain-self]
    - WARN  | xcodebuild:  /Users/chenyuan/ComAlertDialogTool/ComAlertDialogTool/Classes/ComToastView.m:148:9: warning: block implicitly retains 'self'; explicitly mention 'self' to indicate this is intended behavior [-Wimplicit-retain-self]

[!] ComAlertDialogTool did not pass validation, due to 11 warnings (but you can use `--allow-warnings` to ignore them).
You can use the `--no-clean` option to inspect any issue.    
//没有通过验证,发现是有很多警告,我们可以去修改警告,或者使用pod lib lint --allow-warnings
chenyuandeMacBook-Pro:ComAlertDialogTool chenyuan$ pod lib lint --allow-warnings

 -> ComAlertDialogTool (0.0.1)
    - WARN  | summary: The summary is not meaningful.
    - WARN  | xcodebuild:  /Users/chenyuan/ComAlertDialogTool/ComAlertDialogTool/Classes/ComAlertView.m:205:10: warning: block implicitly retains 'self'; explicitly mention 'self' to indicate this is intended behavior [-Wimplicit-retain-self]
    - WARN  | xcodebuild:  /Users/chenyuan/ComAlertDialogTool/ComAlertDialogTool/Classes/ComAlertView.m:243:9: warning: block implicitly retains 'self'; explicitly mention 'self' to indicate this is intended behavior [-Wimplicit-retain-self]
    - WARN  | xcodebuild:  /Users/chenyuan/ComAlertDialogTool/ComAlertDialogTool/Classes/ComAlertView.m:276:9: warning: block implicitly retains 'self'; explicitly mention 'self' to indicate this is intended behavior [-Wimplicit-retain-self]
    - WARN  | xcodebuild:  /Users/chenyuan/ComAlertDialogTool/ComAlertDialogTool/Classes/ComAlertView.m:277:9: warning: block implicitly retains 'self'; explicitly mention 'self' to indicate this is intended behavior [-Wimplicit-retain-self]
    - WARN  | xcodebuild:  /Users/chenyuan/ComAlertDialogTool/ComAlertDialogTool/Classes/ComLoadingView.m:142:13: warning: block implicitly retains 'self'; explicitly mention 'self' to indicate this is intended behavior [-Wimplicit-retain-self]
    - WARN  | xcodebuild:  /Users/chenyuan/ComAlertDialogTool/ComAlertDialogTool/Classes/ComLoadingView.m:306:20: warning: incompatible pointer types returning 'UIView *' from a function with result type 'ComLoadingView *' [-Wincompatible-pointer-types]
    - WARN  | xcodebuild:  /Users/chenyuan/ComAlertDialogTool/ComAlertDialogTool/Classes/ComLoadingView.m:317:20: warning: incompatible pointer types returning 'UIView *' from a function with result type 'ComLoadingView *' [-Wincompatible-pointer-types]
    - WARN  | xcodebuild:  /Users/chenyuan/ComAlertDialogTool/ComAlertDialogTool/Classes/ComToastView.m:116:9: warning: block implicitly retains 'self'; explicitly mention 'self' to indicate this is intended behavior [-Wimplicit-retain-self]
    - WARN  | xcodebuild:  /Users/chenyuan/ComAlertDialogTool/ComAlertDialogTool/Classes/ComToastView.m:147:9: warning: block implicitly retains 'self'; explicitly mention 'self' to indicate this is intended behavior [-Wimplicit-retain-self]
    - WARN  | xcodebuild:  /Users/chenyuan/ComAlertDialogTool/ComAlertDialogTool/Classes/ComToastView.m:148:9: warning: block implicitly retains 'self'; explicitly mention 'self' to indicate this is intended behavior [-Wimplicit-retain-self]

ComAlertDialogTool passed validation.
chenyuandeMacBook-Pro:ComAlertDialogTool chenyuan$ 

接下来我们把Spec提交到官方的CocoaPods的Specs Repo
提交之前,你需要注册pod账号。实质上是将"邮箱--名称--电脑"绑定在一起,所以这里不需要密码。
按照执行之后的提示,需要我们去登录邮箱验证邮件。


chenyuandeMacBook-Pro:ComAlertDialogTool chenyuan$ pod trunk register 3023056944@qq.com ychen3022
[!] Please verify the session by clicking the link in the verification email that has been sent to 3023056944@qq.com
chenyuandeMacBook-Pro:ComAlertDialogTool chenyuan$ 

然后,尝试提交pod库,执行pod trunk push ComAlertDialogTool.podspec --allow-warnings
出现下面的命令则说明提交到官方CocoaPods已经成功,
可是用pod search搜索可能不会搜索到,原因可能有延迟,需要等待一个小时左右,才会搜到。

chenyuandeMacBook-Pro:ComAlertDialogTool chenyuan$ pod trunk push ComAlertDialogTool.podspec --allow-warnings
Updating spec repo `master`
Validating podspec
 -> ComAlertDialogTool (0.0.1)
    - WARN  | summary: The summary is not meaningful.
    - WARN  | xcodebuild:  ComAlertDialogTool/ComAlertDialogTool/Classes/ComAlertView.m:205:10: warning: block implicitly retains 'self'; explicitly mention 'self' to indicate this is intended behavior [-Wimplicit-retain-self]
    - WARN  | xcodebuild:  ComAlertDialogTool/ComAlertDialogTool/Classes/ComAlertView.m:243:9: warning: block implicitly retains 'self'; explicitly mention 'self' to indicate this is intended behavior [-Wimplicit-retain-self]
    - WARN  | xcodebuild:  ComAlertDialogTool/ComAlertDialogTool/Classes/ComAlertView.m:276:9: warning: block implicitly retains 'self'; explicitly mention 'self' to indicate this is intended behavior [-Wimplicit-retain-self]
    - WARN  | xcodebuild:  ComAlertDialogTool/ComAlertDialogTool/Classes/ComAlertView.m:277:9: warning: block implicitly retains 'self'; explicitly mention 'self' to indicate this is intended behavior [-Wimplicit-retain-self]
    - WARN  | xcodebuild:  ComAlertDialogTool/ComAlertDialogTool/Classes/ComLoadingView.m:142:13: warning: block implicitly retains 'self'; explicitly mention 'self' to indicate this is intended behavior [-Wimplicit-retain-self]
    - WARN  | xcodebuild:  ComAlertDialogTool/ComAlertDialogTool/Classes/ComLoadingView.m:306:20: warning: incompatible pointer types returning 'UIView *' from a function with result type 'ComLoadingView *' [-Wincompatible-pointer-types]
    - WARN  | xcodebuild:  ComAlertDialogTool/ComAlertDialogTool/Classes/ComLoadingView.m:317:20: warning: incompatible pointer types returning 'UIView *' from a function with result type 'ComLoadingView *' [-Wincompatible-pointer-types]
    - WARN  | xcodebuild:  ComAlertDialogTool/ComAlertDialogTool/Classes/ComToastView.m:116:9: warning: block implicitly retains 'self'; explicitly mention 'self' to indicate this is intended behavior [-Wimplicit-retain-self]
    - WARN  | xcodebuild:  ComAlertDialogTool/ComAlertDialogTool/Classes/ComToastView.m:147:9: warning: block implicitly retains 'self'; explicitly mention 'self' to indicate this is intended behavior [-Wimplicit-retain-self]
    - WARN  | xcodebuild:  ComAlertDialogTool/ComAlertDialogTool/Classes/ComToastView.m:148:9: warning: block implicitly retains 'self'; explicitly mention 'self' to indicate this is intended behavior [-Wimplicit-retain-self]

Updating spec repo `master`

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

 🚀  ComAlertDialogTool (0.0.1) successfully published
 📅  May 1st, 23:58
 🌎  https://cocoapods.org/pods/ComAlertDialogTool
 👍  Tell your friends!
--------------------------------------------------------------------------------
chenyuandeMacBook-Pro:ComAlertDialogTool chenyuan$ 

注:本文参考Miaoz0070的CocoaPods制作第三方代码库,发布到官方Pod和自己的私有库(模块化、组件化)
链接:https://www.jianshu.com/p/f218fe3baff8

CocoaPods的中高级用法:https://www.jianshu.com/p/d6a592d6fced

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

推荐阅读更多精彩内容