很多时候,自己写了一个小工具,希望在自己的每个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位置添加自己的代码。
注意:听说这个里面最多只能有一层文件夹。
添加完代码后,记得查看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仓库上创建一个工程
然后执行下面的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