Cocoapods搭建私有库
2022-05-12 本文已影响0人
LUJQ
随着公司项目越来越多,不同项目间会有很多相同的功能代码。比如:网络获取、信息弹框、登陆等,将这些封装成模块module做成Cocoapods私有库或者Framework就很有必要了。这儿我想简单记录下Cocoapods私有库的搭建,如有错误 欢迎指正。
Framework 见我另一篇文章:ios Framework制作 (和踩过的坑)
搭建Cocoapods私有库
1、环境
Cocoapods环境,具体环境的配置过程其他文章有很多,就不说了。
2、搭建
私有库搭建组要有两部分:创建 repo 私有库的索引库 spec、创建私有库并发布到索引库。
<1>创建 repo 私有库的索引库 spec
在git上创建索引库 spec,我这儿用码云举例,方法和创建项目是一样的。
![](https://img.haomeiwen.com/i1489590/82ca9836456d936b.png)
然后就有了索引库的地址:https://gitee.com/*******/test-module-specs.git
![](https://img.haomeiwen.com/i1489590/399efd52b225dfcd.png)
将索引库添加到本地仓库
// pod repo add specs库名 specs库地址
pod repo add TestModuleSpecs https://gitee.com/******/test-module-specs.git
查看是否添加成功
pod repo list
//可以看到已经添加成功
TestModuleSpecs
- Type: git (master)
- URL: https://gitee.com/*********/test-module-specs.git
- Path: /Users/*******/.cocoapods/repos/TestModuleSpecs
<2>创建私有库并发布到索引库
第一步:先在git上创建私有库,还是以码云举例。
![](https://img.haomeiwen.com/i1489590/e261aa1036470084.png)
然后就有了私有库地址:
https://gitee.com/*************/test-module-one.git
第二步:创建私有库模板
//创建私有库模板
pod lib create TestModuleOne
//下面是私有库的简单配置
//选择平台
What platform do you want to use?? [ iOS / macOS ]
> ios
//选择编程语言
What language do you want to use?? [ Swift / ObjC ]
> objc
//选择是否创建测试demo
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?
> LU
这就得到了库模板:
![](https://img.haomeiwen.com/i1489590/5bdda97f64aea9c4.png)
这就创建好了私有库模块了
编辑私有库索引 TestModuleOne.podspec
version 功能版本,建议和tag保持一致,版本从0.1.0开始。
summary 功能概要,需要填写更新,否则lint检测无法通过。
description 功能描述,可以选择性删除,否则lint检测无法通过。
homepage 资源首页,私有库对应的浏览器地址。
resource 资源地址,git克隆地址。建议使用http/https,git类型有权限控制。
source_file 类资源文件,默认Classes下的所有文件,放置私有库核心文件。
resources Bundle资源文件(不推荐使用),会合并至MainBundle中,访问便利,但会存在命名冲突问题。个别SDK必须放在MainBundle中才能使用,比如微博SDK!!!。
resource_files Bundle资源文件(推荐使用),单独的Bundle文件,不与MainBundle合并,使用内部资源时和MainBundle路径有区别!!!。
exclude_files 指定不被包含的文件、目录
vendored_libraries 内部包含的.a静态库 例如'ModuleName/Classes/Lib/*.{a}'
vendored_framework 内部包含的.framework静态库 例如'ModuleName/Classes/Framework/***.framework'
static_framework 指定pod加静态库标签 true/false
指定支持的架构,如果因为i386等架构问题lint检测不通过,可以在检测时添加 --skip-import-validation参数
s.xcconfig = {
'VALID_ARCHS' => 'armv7 arm64e armv7s arm64 x86_64',
}
如果支持单文件目录下的文件引用,可以设置subspec
s.default_subspec = 'Core'
s.subspec 'Core' do |core|
core.dependency 'MBProgressHUD'
core.source_files = "DYFoundationFramework/Classes/**/*.{h,m}"
end
s.subspec 'OldCommonTools' do |oct|
oct.dependency 'SAMKeychain'
oct.source_files = "DY****Framework/Classes/Object-C/DY****Tools/**/*.{h,m}"
end
![](https://img.haomeiwen.com/i1489590/e1e5daa3278feb4e.png)
验证.podspec文件的格式是否正确
pod lib lint
//本地验证pod能否通过验证,如果失败使用下面命令: pod lib lint --verbose查看原因,或者使用pod lib lint --allow-warnings忽略警告错误
将私有库代码提交到git
git remote add origin https://gitee.com/********/test-module-one.git
git add .
git commit -a -m "第一次提交 版本为0.1.0"
git pull origin master --allow-unrelated-histories
git push -f origin master
git tag 0.1.0
git push origin 0.1.0
podspec文件中的地址要和远程仓库保持一致
git push -f origin master,本地强制上传到远程,把远程的覆盖,这儿是第一次上传,所有就用本地代码覆盖掉远端代码了。
这儿就已经吧私有库代码提交到git上了
第三步:将私有库发布
//pod repo push 索引库名 私有库.podspec
pod repo push TestModuleSpecs TestModuleOne.podspec --allow-warnings
//检查一下是否成功
pod search TestModuleOne
-> TestModuleOne (0.1.0)
A short description of TestModuleOne.
pod 'TestModuleOne', '~> 0.1.0'
- Homepage: https://gitee.com/********/test-module-one
- Source: https://gitee.com/******/test-module-one.git
- Versions: 0.1.0 [TestModuleSpecs repo]
这就算是搭建完成了