CocoaPods制作第三方代码库,公共远程库
[toc]
前言
Cocoapods 是个很好用的三方库管理工具,一直用的别人的三方库,突然想自己也搞一个三方库试试,查了许多方法,终于成功了,下面记录方法步骤。
环境安装
首先我们需要安装CocoaPods环境,之前写过CocoaPods环境安装,没安装的同学可以跟着安装下。最好安装最新的环境,否则执行下边步骤可能会出现错误。
制作本地库
打开目录,cd到指定目录,输入指令创建库。LFToolPod
是库的名字。
pod lib create LFToolPod
输入命令后会显示下载模板,会有几秒钟等待。
Cloning `https://github.com/CocoaPods/pod-template.git` into `LFToolPod`.
模板下载好后 需要配置以下问题
Configuring LFToolPod template.
! Before you can create a new library we need to setup your git credentials.
首先设置邮箱账号
What is your email?
#设置邮箱
然后回答以下问题
------------------------------
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测试?(我一直都是NO,没有测试过)
> no
What is your class prefix?
#类名前缀是什么?(自定义 我填名字缩写)
> HX_
Running pod install on your new library.
接下来会自动执行pod install。
Analyzing dependencies
Fetching podspec for `LFToolPod` from `../`
Downloading dependencies
Installing LFToolPod (0.1.0)
Generating Pods project
Integrating client project
[!] Please close any current Xcode sessions and use `LFToolPod.xcworkspace` for this project from now on.
Sending stats
Pod installation complete! There is 1 dependency from the Podfile and 1 total pod installed.
[!] Automatically assigning platform `ios` with version `9.3` on target `LFToolPod_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 'LFToolPod/Example/LFToolPod.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`.
然后会自动打开程序,这是一个空的本地库,你会看到如下目录结构
注意点:
1、我们自己的代码不管是新添加文件、还是现有代码有改动,都需要需要在cd到Example下执行pod install后,才能导入头文件import ZMBase,代码才会更新。
2.我们自己的代码文件最多一层文件结构即Animation文件夹内只能放代码,不能再创建子文件夹。
3.每个类文件如果想暴露出去,则需要在代码类名前添加修饰符即(public class xxx或者open extension xxx),添加public、open,而extension不能用open修饰所以用plublic修饰。
4.如果已经暴露的类,想暴露里边的函数方法,则也需要在方法 func 前加public,否则外部调用会找不到方法名。
本地库提交到CocoaPods远程仓库
查看生成的podspec
文件 文件中描述的该库的版本信息,如库的名字、版本号、描述、依赖库等等。
#
# Be sure to run `pod lib lint LFToolPod.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 = 'LFToolPod'
#库版本
s.version = '1.0.0'
#库简短描述
s.summary = 'A short description of LFToolPod.'
# 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/Coldcrack/LFToolPod'
# s.screenshots = 'www.example.com/screenshots_1', 'www.example.com/screenshots_2'
#库开源许可
s.license = { :type => 'MIT', :file => 'LICENSE' }
#作者信息
s.author = { 'Coldcrack' => '13225542075@163.com' }
#源码git地址
s.source = { :git => 'https://github.com/Coldcrack/LFToolPod.git', :tag => s.version.to_s }
# s.social_media_url = 'https://twitter.com/<TWITTER_USERNAME>'
#库依赖系统版本
s.ios.deployment_target = '8.0'
#“*” 表示匹配所有文件
#“*.{h,m}” 表示匹配所有以.h和.m结尾的文件
#“**” 表示匹配所有子目录
#源码文件配置
s.source_files = 'LFToolPod/Classes/**/*'
#资源文件配置
# s.resource_bundles = {
# 'LFToolPod' => ['LFToolPod/Assets/*.png']
# }
#源码头文件配置
# s.public_header_files = 'Pod/Classes/**/*.h'
#系统框架依赖
# s.frameworks = 'UIKit', 'MapKit'
#依赖第三方库
# s.dependency 'AFNetworking', '~> 2.3'
end
Specs Repo:是Git库存放Specs文件,它包含了所有可用的第三方库。每个库都会有独立的文件夹,每个版本以独立的子文件夹保存。文件夹中只有一个描述文件,也就是Spec。
Code:就是我们自己库的源代码。
Code Repo:就是Git库存放源代码的地方。
*注意:Specs Repo和Code Repo是不一样的,存放的地址也不一样,不能存放到同个git库下
提交到官方的CocoaPods
我们cd 到 有podspec
后缀的文件的目录下
1.提交源代码到git
我们在github上建立个远程库用来存放代码,也可以是码云什么的
error 是库名已存在,让再换一个
git remote add origin https://github.com/Coldcrack/LFToolPod
git add .
git commit -a -m "1.0.0"
#git push -u origin master
git push origin maste
git tag 1.0.0
git push origin 1.0.0
hangxiniMac:LFToolPod lvfeng$ git tag 1.0.0
hangxiniMac:LFToolPod lvfeng$ git push origin 1.0.0
Total 0 (delta 0), reused 0 (delta 0)
To https://github.com/Coldcrack/LFToolPod.git
* [new tag] 1.0.0 -> 1.0.0
执行完命令后,这时候已经把代码提交到了git上,如下图:
2.提交Spec文件到Specs Repo
–1>提交到官方的Specs上
首先先用cd到Spec文件所在的目录,然后执行pod lib lint
hangxiniMac:LFToolPod lvfeng$ pod lib lint
-> LFToolPod (1.0.0)
- WARN | summary: The summary is not meaningful.
[!] LFToolPod 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.
hangxiniMac:LFToolPod lvfeng$ pod lib lint --allow-warnings
-> LFToolPod (1.0.0)
- WARN | summary: The summary is not meaningful.
LFToolPod passed validation.
库没有通过验证,说明Spec配置有问题,在Spec出现warnings是不行的,我们可以根据警告进行修改,也可以我们执行pod lib lint –allow-warnings
若出现下面代码,则验证通过
LFToolPod passed validation.
注册pod账号
实质上是将”邮箱–名称–电脑”绑定在一起,所以这里不需要密码。
pod trunk register EMAIL [NAME]
以下是该指令的帮助文档,大概意思是:
如果第一次注册,邮箱和名称都是必须的。如果已经注册过,可以省略名称(除非你想修改名称)。推荐填写描述信息,以及给出了一些Examples,按着Example做就可以了。
pod trunk register EMAIL [NAME]
Register a new account, or create a new session.
If this is your first registration, both an `EMAIL` address and your
`NAME` are required. If you’ve already registered with trunk, you may omit
the `NAME` (unless you would like to change it).
It is recommended that you provide a description of the session, so that
it will be easier to identify later on. For instance, when you would like
to clean-up your sessions. A common example is to specify the location
where the machine, that you are using the session for, is physically
located.
Examples:
$ pod trunk register eloy@example.com 'Eloy Durán' --description='Personal Laptop'
$ pod trunk register eloy@example.com --description='Work Laptop'
$ pod trunk register eloy@example.com
执行
pod trunk register xxx@xx.com Coldcrack
结果
[!] Please verify the session by clicking the link in the verification email that has been sent to xxx@xx.com
去邮箱认证即可。
注册账号之后,我们可以尝试将Spec提交到Specs Repo:
执行命令:
pod trunk push LFToolPod.podspec --allow-warnings
出现下面界面则说明提交到官方CocoaPods已经成功,可以使用pod search
找到提交的库了,如果没有找到,肯能是网络延迟原因,等待1、2小时后再找就好了。
🎉 Congrats
🚀 LFToolPod (1.0.0) successfully published
📅 September 18th, 00:34
🌎 https://cocoapods.org/pods/LFToolPod
👍 Tell your friends!
--------------------------------------------------------------------------------
总结
提交Spec步骤:
1.给项目打tag(必须要打tag,CocoaPods中的不同版本,实质是不同的tag,不打tag会导致下一步验证不通过)
git tag 1.0.0
git push origin 1.0.0
2.检查合法性
pod spec lint LFToolPod.podspec --allow-warnings
结果
-> LFToolPod (1.0.0)
Analyzed 1 podspec.
LFToolPod.podspec passed validation.
3.提交到Specs Repo
pod trunk push LFToolPod.podspec --allow-warnings