每创建一个组件都会带有一个 xxx.podspec的索引文件。专门用来存放这些索引文件的库就叫做索引库。我们需要将这些索引文件上传到远程索引库才能保证其他的人能够拿来用。私有和开源的创建方式一样。
如:https://gitee.com/component-practice/main.git
cd ~/.cocoapods/repos
pod repo add BYPodspecs https://gitee.com/component-practice/bypodspecs.git
输入gitee账号密码后就会在本地生成,可以git pull拉取或者push一下确保可正常提交代码
在本地创建一个文件夹BYH_Components用来存放组件代码,并进入到该路径
cd ~/Desktop/BYH_Components
pod lib create BYHCommon
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
Would you like to do view based testing? [ Yes / No ]
no
What is your class prefix?
BY
Running pod install on your new library.
创建好后会自动打开一个工程,配置xx.podspec文件如下
Pod::Spec.new do |s|
s.name = 'BYHCommon'
s.version = '0.1.0'
s.summary = 'A short description of BYHCommon.'
s.description = <<-DESC
TODO: Add long description of the pod here.
DESC
s.homepage = 'https://gitee.com/Gary0726/lbnetwork'
s.screenshots = 'www.example.com/screenshots_1', 'www.example.com/screenshots_2'
s.license = { :type => 'MIT', :file => 'LICENSE' }
s.author = { 'LuckyBlue' => 'xxxxxxx@qq.com' }
s.source = { :git => 'https://gitee.com/Gary0726/lbnetwork.git', :tag => s.version.to_s }
s.ios.deployment_target = '9.0'
s.source_files = 'BYLogin/Classes/**/*.{h,m}'
s.resource_bundles = {
'BYLogin' => ['BYLogin/Assets/*']
}
s.public_header_files = 'BYLogin/Classes/**/**/*.h'
# 子目录
# s.subspec 'Modules' do |ss|
# ss.source_files = 'BYLogin/Classes/Modules/Controllers/*'
# end
s.static_framework = true
# s.frameworks = 'UIKit', 'MapKit'
s.dependency 'BYHCommon'
# 友盟分享 登录
# s.dependency 'UMCommon'
s.dependency 'UMShare/Social/WeChat'
s.dependency 'YYText'
end
cd /Users/BYH_Components/BYHCommon
git remote add origin https://gitee.com/component-practice/BYHCommon.git
git push -u origin master //第一次可能会报错可尝试用 git push -u origin master -f 可能会覆盖远程的修改
git add . //记得后面一定要有 .
git commit -m "first commit"
git push -u origin master
git tag '0.1.0' //注意:这里的tag号必须和.podSpec文件的版本号一致
git push --tags
本地验证(组件中引用了私有组件,需要加上--sources,再加上--skip-import-validation)
pod lib lint BYH_SFJH.podspec --verbose --allow-warnings --skip-import-validation --sources='https://mirrors.tuna.tsinghua.edu.cn/git/CocoaPods/Specs.git,https://gitee.com/component-practice/bypodspecs.git'
远端验证
pod spec lint BYH_SFJH.podspec --verbose --allow-warnings --skip-import-validation --sources='https://mirrors.tuna.tsinghua.edu.cn/git/CocoaPods/Specs.git,https://gitee.com/component-practice/bypodspecs.git'
验证时如有遇到fatal: The remote end hung up unexpectedly那可能是网络有问题,自己起初用的github一直试都报这个错,尽管用了git config http.postBuffer 524288000
常见报错:加上 --verbose 可显示详细报错信息
cd到组件目录:
pod repo push BYPodspecs BYHCommon.podspec --use-libraries --allow-warnings --skip-import-validation
BYPodspecs: ~/.cocoapods/repos/BYPodspecs
BYHCommon.podspec: 组件的podspec文件
如果报错需要查看 ~/.cocoapods/repos 中的podsec文件是否成功关联远程库,是否能push/pull远程库 查看步骤:3.索引库关联本地 是否正确
pod search BYHCommon
注:发布完成之后使用pod search组件名称却搜不到相关的信息,这个时候需要将cocoaPods的搜索缓存文件清理一下,然后重新使用pod search,就能得到最新的结果。
//进入搜索CocoaPods缓存目录
cd ~/Library/Caches/CocoaPods
//查看缓存文件search_index.json
ls
//删除搜索缓存文件
rm -f search_index.json
//删除本地tag
git tag -d 0.1.0
//删除远程tag
git push origin :refs/tags/0.1.0
//查看本地添加了哪些远程地址
git remote -v
//删除本地指定的远程地址
git remote rm origin
如果公司内部其他成员要使用需要执行下面命令,将私有的远程索引库copy到本地,然后就可以正常使用了。
pod repo add BYPodspecs https://gitee.com/component-practice/bypodspecs.git
参考此链接,包含所有报错:
https://www.jianshu.com/p/87d6034d3878