iOS 搭建组件化私有库

一、创建私有库索引

步骤1是在没有索引库的情况下或者是新增索引的时候才需要用到(创建基础组件库)

首先在码云上建立一个私有库索引,起名为SYComponentSpec

二、本地添加私有库索引

添加私有库索引

bash 复制代码
pod repo add SYComponentSpec https://gitee.com/sun-shiyu/sycomponent-spec

三、创建基础组件库

当你需要新建组件的时候,就在这里开始,如果是想修改原有的库,就可以直接在之前的组件仓库里面修改即可

1.在码云上创建组件库

命名为SYBasicComponents,如图:


2.创建SYBasicComponents本地库

默认创建路径:/Users/sunshiyu/SYBasicComponents 。

bash 复制代码
pod lib create SYBasicComponents

最后,项目本地组件库创建完成后会自动打开项目。

3.在私有库导入自己的代码

将Classes文件夹下面的ReplaceMe.m文件删除掉,替换成你要上传私有库的代码,这里导入简单的两个测试文件:

objectivec 复制代码
// SYLog.h
+ (void)logger;
objectivec 复制代码
// SYLog.m
+ (void)logger {
    NSLog(@"-------- Log --------");
}

4.更新这个工程的pod库

  1. cd 到 Example文件下
  2. 执行 pod install

5.修改.podspec文件


有两个属性需要注意改下:
s.homepage:私有代码仓库的地址 https://gitee.com/sun-shiyu/sybasic-components
s.source:私有代码仓库的源地址 https://gitee.com/sun-shiyu/sybasic-components.git

其他属性根据需要自行配置。

6.将私有库push到远程仓库

注意远端需要有个master分支,这里先创建一个master分支:

bash 复制代码
 cd /Users/sunshiyu/SYBasicComponents 
bash 复制代码
git branch master
bash 复制代码
git checkout master

commit代码到本地然后push到远端:

bash 复制代码
 git branch (检查当前所在分支)
bash 复制代码
git status (查看当前git存了什么文件)
bash 复制代码
git add . (将所有文件缓存到待提交文件区域)
bash 复制代码
git commit -m "上传组件"
bash 复制代码
git remote add origin https://gitee.com/sun-shiyu/sybasic-components.git  (私有库git仓库地址)
bash 复制代码
git push -f origin master (将代码推送到远程私有库git仓库的master分支上面已经创建了)
bash 复制代码
git tag 0.1.0 (这里的版本号必须和podspec里面写的版本号一致)
bash 复制代码
git push -- tags (提交tag)


7.本地和远程校验

1.本地校验,在当前私有库目录下,输入命令:

bash 复制代码
pod lib lint --private --allow-warnings

校验成功:SYBasicComponents passed validation.

2.远程验证,在当前私有库目录下,输入命令:

bash 复制代码
pod spec lint --private --allow-warnings

校验成功:SYBasicComponents.podspec passed validation.

8.提交索引文件到远程索引库

1.所有验证通过之后,要将spec文件推送到最开始创建的私有库索引库当中

cd 到私有库项目目录,
pod repo push <本地索引库名称> <索引文件名> --allow-warnings
<本地索引库名称>在 /Users/sunshiyu/.cocoapods/repos 下的私有库索引项目名称
<索引文件名>就是以 podspec 结尾的,注意不要弄错

例如输入命令:

bash 复制代码
pod repo push SYComponentSpec SYBasicComponents.podspec --allow-warnings

2.推送去之后,在本地索引库中查看如下图​​​​​​​​​​​​​​:

在 getee 远端查看如下图:

四、使用基础组件库

随便创建一个项目名为 SYSpecDemo ,初始化 pod :

bash 复制代码
 cd /Users/sunshiyu/Desktop/SYSpecDemo
bash 复制代码
pod init
bash 复制代码
pod install

打开编辑 podfile 文件

bash 复制代码
open podfile

如下:

bash 复制代码
# Uncomment the next line to define a global platform for your project
platform :ios, '11.0'

# source 添加对应的索引库,否则会pod install失败
source 'https://gitee.com/sun-shiyu/sycomponent-spec.git'

target 'SYSpecDemo' do
  # Comment the next line if you don't want to use dynamic frameworks
  use_frameworks!

  # Pods for SYSpecDemo
  
  # pod 对应组件,可以对应版本
  pod 'SYBasicComponents'

end

再次:

bash 复制代码
pod install

就可以看到这个库:

导入头文件使用该库:

objectivec 复制代码
#import <SYBasicComponents/SYLog.h>

// 打印 log 
[SYLog logger];

五、组件库依赖第三方库

如果我们的组件库需要依赖第三方库,例如 AFNetWorking、YYModel 等,操作如下:

1.修改组件库的 SYBasicComponents.podspec 文件,添加依赖:

然后组件库执行 Example 项目 执行 pod install。

2.同上面第6步,将组件库修改的代码提到远端。记住一定要记得打 tag,且要与索引库的 version 保持一致 。

bash 复制代码
git tag 0.2.0 (这里的版本号必须和podspec里面写的版本号一致)
bash 复制代码
git push -- tags (提交tag)

3.提交索引库远端,同上面的第8步:

bash 复制代码
pod repo push SYComponentSpec SYBasicComponents.podspec --allow-warnings

4.修改主项目的 podfile 文件,加入 github CocoaPods 索引库,用来加载github三方库的:

bash 复制代码
source 'https://github.com/CocoaPods/Specs.git'

然后执行 pod install。

以上是使用 pod 'SYBasicComponents' 的方式使用组件库,也就是远程的方式,这种方式需要没更新组件库后都要打 tag 以及修改 spec 的version,然后修改主项目的 podfile 文件(根据情况也可能不需要要修改),最后主项目重新 pod install 。这样就能得到远程组件库最新的代码,缺点是主项目每次想更新组件库的最近代码都需要组件库打 tag 更新组件库的版本,如何实现只要组件库有代码更新,主项目直接拉最新的代码呢?答案是引用本地索引库的方式,我们只需要修改主项目的 podfile 文件如下:

objectivec 复制代码
# Uncomment the next line to define a global platform for your project
platform :ios, '11.0'

# source 添加对应的索引库,否则会 pod install 失败
# source 'https://gitee.com/sun-shiyu/sycomponent-spec.git'
source 'https://github.com/CocoaPods/Specs.git'

target 'SYSpecDemo' do
  # Comment the next line if you don't want to use dynamic frameworks
  use_frameworks!

  # Pods for SYSpecDemo
  
  # pod 对应组件,可以对应版本
  
  # 方式一、引用远端组件库
  # pod 'SYBasicComponents'
  
  # 方式二:引用本地组件库,不需要 source 'https://gitee.com/sun-shiyu/sycomponent-spec.git'
  pod 'SYBasicComponents', :path => '~/SYBasicComponents/'
 
end

即使用 pod 'SYBasicComponents', :path => '~/SYBasicComponents/' 的方式来直接引用本地组件库的代码,这样拉代码每次都是最新的。我们 pod install 试试看,Pods 目录结构变如下图所示:

本地组件库 SYBasicComponents 里面就是最新的代码了。因为我们的组件库依赖了 AFNetworking YYModel,所以Pods中出现了这两个库。

结束!!!

参考:
iOS私有库搭建
iOS组件化搭建私有库

相关推荐
茶底世界之下4 天前
为什么预览、录制、离线导出不能共享同一个背压策略?
ios·swift
2501_916007475 天前
苹果商店iOS应用上架费用全面解析:开发者计划与审核费用计算
android·ios·小程序·https·uni-app·iphone·webview
2501_916008895 天前
如何实现iOS App代码混淆:SwiftShield使用指南
android·ios·小程序·https·uni-app·iphone·webview
2501_915909065 天前
移动端开发工具链怎么组合比较好,iOS、Android、跨端三套配置方案
ide·vscode·ios·objective-c·个人开发·swift·敏捷流程
2501_916008895 天前
怎么用 Godot 导出 iOS 应用并上架 App Store?签名字段该填什么?
android·ios·小程序·https·uni-app·iphone·webview
2501_915918415 天前
iOS编程用什么软件好?主流工具Xcode、AppCode、CodeRunner与新兴快蝎对比
ide·vscode·ios·objective-c·个人开发·swift·敏捷流程
终端安全笔记5 天前
iOS 27 之后「策略空转」:设备升级不报错,但旧策略不再管它
android·网络·安全·ios·智能手机
9765033356 天前
iOS 上架 4.3a 被拒【uniapp专讲】
flutter·ios·objective-c·uniapp·swift
黑化旺仔6 天前
【OC】KVO
macos·ios·objective-c·cocoa
HouWan6 天前
SwiftUI 小技巧:如何读取 SwiftUI Font 的详细信息
ios·swiftui·apple