一、创建组件库
-
github.com新建基础组件库,这里以FloatButton为例
image
-
本地合适位置新建与组件库同名的文件夹FloatButton
image
cd进去之后,使用如下命令:pod lib create FloatButton
执行完成之后,应该是如下展示:

这里根据各项目实际情况选择即可。
原来的FloatButton文件夹里会多出一个新项目文件夹,也叫FloatButton,如图所示:

如果执行成功,会自动打开FloatButton-Example项目:

- 添加组件内容
打开FloatButton项目文件夹里的Classes,将封装的代码库文件放置其中(这里我是放置FloatButton.swift),并且删除默认创建的空Swift文件ReplaceMe.swift
image
默认Classes文件夹下面就是pod install
下来的文件。 - 安装与测试本地库
cd至FloatButton文件夹下的Example项目,执行pod install
image
如此,在Example项目的Pods里可以看到刚才新增的FloatButton.swift文件
image
测试组件没有问题后,我们接下来就要将podspec文件上传至私有索引库,不过在此之前,需要对spec进行修改。
-
修改FloatButton.podspec文件
image
先看下FloatButton.podspec文件里的内容
image
这里需要修改的几个参数为:
s.name = 'FloatButton'
s.version = '0.1.0'
s.summary = '悬浮按钮 FloatButton'
# 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
自定义悬浮按钮,可随手势在屏幕任意位置滑动,类似于苹果手机自带辅助触控按钮!Swift5.0版本
DESC
s.homepage = 'https://github.com/XXX/FloatButton'
# s.screenshots = 'www.example.com/screenshots_1', 'www.example.com/screenshots_2'
s.license = { :type => 'MIT', :file => 'LICENSE' }
s.author = { 'XXX' => 'XXX@gmail.com' }
s.source = { :git => 'https://github.com/XXX/FloatButton.git', :tag => s.version.to_s }
# s.social_media_url = 'https://twitter.com/<TWITTER_USERNAME>'
s.ios.deployment_target = '8.0'
s.source_files = 'FloatButton/Classes/**/*'
s.homepage
需要修改为远程私有库仓库首页地址;
s.source
需要修改为远程私有库的git地址;
二、上传组件库
-
代码提交仓库之前,需要配置好ssh key,参考文章Mac管理多个ssh key。
-
将代码提交至组件仓库:
git init
git add .
git commit -m 'firstCommit'
git remote add origin git@github.com:XXX/FloatButton.git
git push -u origin master
- 配置Tag:
- 标签0.1.0与spec中的s.version保持一致
// 新建tag
git tag 0.1.0
// push tag至远程
git push origin --tags
-
配置成功在github上的展示:
image
三、验证私有库:
1、本地验证:
/// 这里要注意,如果不指定source,则会报 [!] Found multiple specifications for 'xxx'的错误。远程校验亦是如此。
pod lib lint --private --sources='https://github.com/CocoaPods/Specs.git'
验证成功如图示:

2、远程验证:
pod spec lint --sources='https://github.com/CocoaPods/Specs.git'
验证成功如图示:

四、踩坑记录
1、本地验证Spec
pod lib lint --private --sources='https://github.com/CocoaPods/Specs.git'
报错如下:

解决方案:截图所示很清楚的指明了未指定Swift语言版本,这里我用的Swift5.0版本,故而需要在.podspec文件里加上
s.swift_versions = '5.0'
,并且在组件库工程设置开发语言版本为Swift 5
参考链接:
网友评论