浅谈CocoPods组件化添加私有库:(一)

一、创建私有库的中心仓库

1.1、这个我们先大概讲一下原理:
这个也可以称为spec库, 这个库存储的就是我们上边在A工程中创建的 podspec 文件, 我们在实际的项目中, 比如我们要加入我们写的A工程的代码. 就需要写 pod “A” , 然后执行 pod install , 这时pod就会去这个中心仓库里找到 A 的 podspec 文件, 然后从这个文件中读取相关的信息, 比如从哪个远程库拉代码, 拉哪些代码, 版本是什么, 都需要依赖其它哪些库等等.所以这个库就是⽤来存 podspec ⽂件的, 可以存很多, 不只是 A.podsepc.
1.2、创建中心仓库过程:
1. 创建一个库, 并创建其相应的远程仓库, 比如库的名字叫 GB-iOS-SPEC, 对应远程地址为:https://gitlab.com/gaobingking/gb-ios-spec.git

在这里插入图片描述

创建成功后可以新增README.md说明文件

在这里插入图片描述

2. 我们先将这个库加到我们本地存放这些仓库的地方, 一般这个位置在 ~/.cocoapods/ 下边, 会存在一个master仓库, 是cocoapods⾃己的主仓库;
  • 查看本地的cocopods的spec仓库:
pod repo
  • 现在我们把⾃己的 GB-iOS-SPEC 添加进去, 运⽤用命令:
pod repo add GB-iOS-SPEC https://gitlab.com/gaobingking/gb-ios-spec.git 
这样我们就在~/.cocoapods 下新增了了自己的 GB-iOS-SPEC 仓库:

在这里插入图片描述

二、创建CocoPods的私有库

2.1、创建私有库的远程仓库GBPodDemo;
2.2、创建本地的私有库GBPodDemo;
2.2.1、创建过程
pod lib create GBPodDemo
创建的时候从CocoaPods库下载模板
Cloning `https://github.com/CocoaPods/pod-template.git` into `GBPodDemo`.
Configuring HotelTest template.
创建私有库时候的提示
If this is your first time we recommend running through with the guide: 
 - https://guides.cocoapods.org/making/using-pod-lib-create.html
创建前的几个配置项选择:
  • What platform do you want to use?? [ iOS / macOS ]
    iOS
    第一个问题是选择开发的平台是iOS还是macOS构建项目。此教程选的是iOS

  • What language do you want to use? [ Swift / ObjC ]
    ObjC
    第二个问题是问你选择Swift还是Objc构建项目。此教程选的是ObjC

  • Would you like to include a demo application with your library? [ Yes / No ]
    Yes
    第三个问题问你是否需要创建一个Demo项目,此教程选的是Yes

  • Which testing frameworks will you use? [ Specta / Kiwi / None ]
    Specta
    第四个问题让你是否选择一个测试框架,此教程选 Specta

  • Would you like to do view based testing? [ Yes / No ]
    Yes
    第五个问题是否基于View测试,选Yes

  • What is your class prefix?
    GB
    第六个问题是询问类的前缀,设为GB

2.2.2、创建成功后会在目录中创建好一个GBPodDemo工程,结构如下:

在这里插入图片描述

把Classes文件夹里面的ReplaceMe.m文件删掉,替换成自己的代码。
2.3、本地仓库与远程仓库关联:
2.3.1、由于是用的gitlab的私有库,推送前先检测该电脑的SSH公钥是否已经添加到gitlab的账户中,没有的话添加下,参考gitlab添加SSH公钥的博客;
2.3.2、开始关联
git remote add origin git@gitlab.com:gaobingking/gbpoddemo.git
git add .
git commit -m "Initial commit"
git push -u origin master

三、配置GBPodDemo.podspec文件并提交到私有Repo仓库

3.1、在配置spec文件时,由于涉及到私有库的版本号配置,配置的版本号必须是tag的版本才行,所以在配置spec之前必须先将工程打一个对应的tag版本;
3.2、配置spec文件
3.2.1 spec文件的基础配置

在这里插入图片描述

3.2.2 私有库中资源文件或依赖库的配置
  • 图片及xib资源

在这里插入图片描述

s.resource_bundles = {
    'GBPodDemo' => ['GBPodDemo/Assets/*']
  }
  • 私有库中读取图片的方式:
+(UIImage*)slatImageNamed:(NSString*)imageName {
    NSString *path = [[NSBundle mainBundle] pathForResource:@"GBPodDemo" ofType:@"bundle"];
    NSBundle *bundle = [NSBundle bundleWithPath:path];
    UIImage *img = [UIImage imageNamed:imageName inBundle:bundle compatibleWithTraitCollection:nil];
    
    return img;
}
  • 依赖库中有静态库:

boss->A->B->C(static library)

boss为主项目

SL为包含.a的静态库

boss->C,没有问题

在B内增加

 s.static_framework = true

可以解决boss->B->C问题

  • 增加自有framework

如果有动态库需要引入使用,将动态库拷⻉到工程, 并用下边的变量指定相应的位置

s.vendored_frameworks = 'GBPodDemo/frameworks/Masonry.framework'

frameworks:为库新建的存放的framework文件夹

  • 增加自有 .a
s.vendored_libraries = GBPodDemo/libraries/*.a

libraries:为库新建的存放的.a文件夹

  • 增加系统静态库

如: libc++.tbd、libz.tbd、libsqlite3.tbd 则进行如下配置: 注意去掉前边的lib和后边的.tbd

s.libraries = 'c++', 'z', 'sqlite3'
  • 增加系统framework

如: GLKit.framework

s.frameworks = 'GLKit'
  • 如需对 other link flags进行配置
s.pod_target_xcconfig = { 'OTHER_LDFLAGS' => '-lObjC' }
3.3、将私有库的spec文件推送到远程中心仓库
3.3.1、验证本地索引文件是否正确
①、如果是github上的公有库
pod lib lint GBPodDemo.podspec --verbose --allow-warnings
②、放到自己服务器的私有库
pod lib lint --sources='https://github.com/CocoaPods/Specs.git,https://gitlab.com/gaobingking/gb-ios-spec.git' --allow-warnings
3.3.2、验证远程索引(可以不用,待验证)
①、如果是github上的公有库
pod spec lint --verbose --allow-warnings
②、放到自己服务器的私有库
pod spec lint --sources='https://github.com/CocoaPods/Specs.git,https://gitlab.com/gaobingking/gb-ios-spec.git' --verbose --allow-warnings
3.3.3、推送到中心仓库
①、如果是github上的公有库
pod repo push GB-iOS-SPEC GBPodDemo.podspec --allow-warnings
②、放到自己服务器的私有库
pod repo push GB-iOS-SPEC --sources='https://github.com/CocoaPods/Specs.git,https://gitlab.com/gaobingking/gb-ios-spec.git' --allow-warnings --verbose
3.3.4、如果引⽤了静态或动态库, 要使⽤:
pod repo push GB-iOS-SPEC GBPodDemo.podspec --allow-warnings --use-libraries
3.3.5、推送失败的问题:

在这里插入图片描述

这个问题是cocopods版本1.5.3的bug,升级cocopods就好了

四、项目中引入私有库

4.1、如果有引入私有库,podfile文件顶部需要添加如下源
source 'https://github.com/CocoaPods/Specs.git'
source 'https://gitlab.com/gaobingking/gb-ios-spec.git'
这样在加载Pod库的时候,才能找到自己的私有库,并且不影响其它公共库的添加。
4.2、添加指定分支的私有库,Podfile文件修改:
pod ‘GBPodDemo' ,:git=> "https://gitlab.com/gaobingking/gbpoddemo.git
t", :branch => ‘podDemo_Dev'

五、项目中指定分支私有库更新

  • 更新私有中心仓库
pod repo update GB-iOS-SPEC 
  • 更新项目中相应的私有库
pod update GBPodDemo --no-repo-update
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值