私有库的步骤:
1.在gitee创建两个库,一个代码库SYYBaseLib_Swift,一个spec库SYYBaseLibSpec_Swift
2.pod lib create SYYBaseLib-Swift创建代码库项目
3.修改podspec,
4.验证podspec
5.提交项目到gitee
6.拉取spec库:pod repo add SYYBaseLibSpec-Swift https://gitee.com/suyongyaogit/SYYBaseLibSpec-Swift.git
7.推送spec库:pod repo push SYYBaseLibSpec_Swift SYYBaseLib_Swift.podspec --allow-warnings
8.测试
创建完两个库就可以开始以下的工作了。
1.创建项目:pod lib create SYYBaseLib_Swift
这个命令行是创建一个Lib项目,用来上传到gitee的项目库的,这句代码会创建好一个Lib的库,自动生成好需要的文件。
创建好项目以后,通常我们会先cd到Example里面去pod install一下确保没问题。
创建好以后打开lib的文件夹(SYYBaseLib_Swift)
SYYBaseLib-Swift文件夹会有Assets和Classes,Assets是存放图片的,Classes是存放自己写的类的,Classes已经自动生成了一个类,可以直接替换成自己的类就行.
但是这里我们先不替换,现在这个类里面先写一个方法,等下pod "SYYBaseLib-Swift"后用来调用一下测试一下,测试通过了我们再替换成自己写的类再上传到gitee来
2.修改SYYBaseLib-Swift.podspec文件
Pod::Spec.new do |s|
s.name = 'SYYBaseLib_Swift'#项目名
s.version = '0.0.1'#版本
s.swift_versions = ['5.0']#验证的时候使用的swift版本
s.summary = '一个Swift的基础框架.'#简介
#s.description详细介绍
s.description = <<-DESC
TODO: 一个Swift的基础框架,包含一些常用的方法,包含类,扩展,和一些原生代码的封装,比如封装一下WebView,SQLite,和Network等
DESC
s.homepage = 'https://gitee.com/suyongyaogit/SYYBaseLib_Swift.git'#项目的主页
# s.screenshots = 'www.example.com/screenshots_1', 'www.example.com/screenshots_2'
s.license = {
:type => 'MIT',
:file => 'LICENSE'
}
#开发者的账号和邮箱,如果是gitee的就写gitee的账号和邮箱就行
s.author = { 'suyongyaogit' => '823895643@qq.com' }
s.source = {
:git => 'https://gitee.com/suyongyaogit/SYYBaseLib_Swift.git',#项目地址,这里一定要对
:tag => s.version.to_s
}
# s.social_media_url = 'https://twitter.com/<TWITTER_USERNAME>'
s.ios.deployment_target = '13.0'#最低版本要求
s.source_files = 'SYYBaseLib_Swift/Classes/**/*.swift'#上传的文件格式
#s.source_files = 'SYYBaseLib_Swift/Classes/**/*.{h,m,swift}'#如果文件里有.h,.m.swift,就这样写
#资源目录
s.resource_bundles = {
'SYYBaseLib_Swift' => ['SYYBaseLib_Swift/Assets/Assets.xcassets'],
}
# s.public_header_files = 'Pod/Classes/**/*.h'
# s.frameworks = 'UIKit', 'MapKit'
s.dependency 'Alamofire', '~> 5.6.4'#依赖库
s.dependency 'HandyJSON'
s.dependency 'SVProgressHUD'
s.dependency 'ESTabBarController-swift'
s.dependency 'FMDB'
s.dependency 'Hue'
s.dependency 'IQKeyboardManagerSwift', '~> 6.5.0'
s.dependency 'Kingfisher'
s.dependency 'MJRefresh'
s.dependency 'SnapKit'
s.dependency 'SwiftDate'
s.dependency 'XHLaunchAd'
end
改完这个文件夹之后会需要验证一下改的对不对,如果验证失败上传到gitee也没用(按照上面中文注释改,一般都会通过),cd到SYYBaseLib-Swift,是Example前面的SYYBaseLib-Swift,执行命令:
pod lib lint --allow-warnings --verbose
如果验证成功会打印
SYYBaseLib-Swift passed validation.
如果失败,就执行命令行:
pod lib lint --allow-warnings --no-clean 查看失败的原因,改好重新验证就行
验证成功会报这个:SYYBaseLib_Swift passed validation.
2.提交项目(或者直接用SourceTree提交也行,记得标签一定要写自己的版本号)
git status
git add .
git commit -m ‘AkTalkKit 库上传’
# 将本地库与远程库进行关联
git remote add origin SYYBaseLib_Swift https://gitee.com/suyongyaogit/SYYBaseLibSpec_Swift.git # 此处为实际git地址
git push origin master
git tag '0.0.1' # 要与实际版本一样
git push --tags
项目库的工作基本完成了,轮到spec索引库
3.拉取索引库(cd 到cd /Users/suyongyao/SYYBaseLib_Swift)
pod repo add SYYBaseLibSpec_Swift https://gitee.com/suyongyaogit/SYYBaseLibSpec_Swift.git
这句命令后会在/Users/suyongyao/.cocoapods/repos自动生成SYYBaseLibSpec_Swift文件夹用于管理spec
4.推送spec
pod repo push SYYBaseLibSpec_Swift SYYBaseLib_Swift.podspec --allow-warnings
这句命令后会自动在/Users/suyongyao/.cocoapods/repos/SYYBaseLibSpec_Swift生成一个文件夹然后用sourcetree把SYYBaseLibSpec_Swift推送到gitee的spec库去
5.完成后就可以测试了,在项目中的Podfile文件中:
# 指定所有 Spec 仓库来源(包括私有和官方)
source 'git@gitee.com:suyongyaogit/SYYBaseLibSpec_Swift.git' # 私有 Spec 仓库#这里要注意建立仓库的时候选择私有库还是公开源的问题,公开库用http格式,私有库用SSH格式
source 'https://github.com/CocoaPods/Specs.git' # 官方仓库
target 'MyApp' do
pod 'SYYBaseLib_Swift', '~> 0.0.1' # 从私有 Spec 仓库解析依赖
end
到这一步这个库应该是可以使用了,但是用着用着你就会发现两个问题:
1.pod install下来的库里面的文件是没有分级的,没有按你自己在本地上分级,而是一股脑的在SYYBaseLib文件下面。
2.自己使用的是swift,而库里面有用到oc的,应该怎么调用
3.版本管理怎么做?
1.文件分层,看SYYBaseLib_Swift.podspec文件里面source_files,使用s.source_files = 'SYYBaseLib_Swift/Classes/*.{h,swift}'
就只会上传全部文件,而不会分级,要想分级要用下面的方法:
s.source_files = 'SYYBaseLib_Swift/Classes/*.{h,swift}'
s.subspec 'Category' do |ss|
ss.source_files = 'SYYBaseLib_Swift/Classes/Category/*.{h,m}'
end
s.subspec 'Macro' do |ss|
ss.source_files = 'SYYBaseLib_Swift/Classes/Macro/*.swift'
end
这样做文件的分层,如果Macro是在Category里面的话,那么关于Macro的文件管理要包含在Category里,如:
s.source_files = 'SYYBaseLib_Swift/Classes/*.{h,swift}'
s.subspec 'Category' do |ss|
ss.source_files = 'SYYBaseLib_Swift/Classes/Category/*.{h,m}'
ss.subspec 'Macro' do |sss|
sss.source_files = 'SYYBaseLib_Swift/Classes/Macro/*.swift'
end
end
这样pod下来的文件就会分好层了,如:
2.swift调用OC
需要使用public_header_files,如:
s.source_files = 'SYYBaseLib_Swift/Classes/*.{h,swift}'
s.public_header_files = 'SYYBaseLib_Swift/Classes/SYYBaseLib.h'
s.subspec 'Category' do |ss|
ss.source_files = 'SYYBaseLib_Swift/Classes/Category/*.{h,m}'
ss.public_header_files = 'SYYBaseLib_Swift/Classes/Category/*.h'
end
s.subspec 'Macro' do |ss|
ss.source_files = 'SYYBaseLib_Swift/Classes/Macro/*.swift'
end
我在Classes下面和Category下面都有OC的代码,那么就要分别在Classes和Category下面使用ss.public_header_files这个属性
3.版本更新
版本更新的时候把需要修改的东西修改好之后,修改SYYBaseLib_Swift.podspec文件的s.version = '0.0.2'#版本
,cd 到Example去pod install
一下,保证上传到gitee的代码和你修改的一致,然后验证:pod lib lint --allow-warnings --no-clean
或者pod lib lint --allow-warnings --verbose
验证完之后会报验证成功:SYYBaseLib_Swift passed validation.
,然后按上面的上传到gitee,/Users/suyongyao/.cocoapods/repos/SYYBaseLibSpec_Swift/SYYBaseLib_Swift文件夹里面新增文件夹,名字就是版本号,如:0.0.2,在代码库SYYBaseLib_Swift中把SYYBaseLib_Swift.podspec拷贝过来,然后上传到到gitee,这样就可以了