美文网首页
CocoaPods组件化集成

CocoaPods组件化集成

作者: yue博客 | 来源:发表于2020-03-28 16:21 被阅读0次

写在前面

一个私有pod库是由代码库和索引组成的,代码库就是具体的实现,存放代码用的,索引是为了pod install时cocoapods可以找到代码库,索引即是 xx.podspec 文件,主要用来配置库的主页,仓库地址,license,文件路径,依赖关系等。

1.创建本地索引库,并关联远程索引库

在终端执行命令

# pod repo add [本地索引库名称(最好与远端索引库库名称保持一致)] [远程索引库地址]
# example 创建一个名为XYAPP的索引库,用于存放XYAPP所用到的所有私有库的索引文件
pod repo add XYAPP http://gitlab/ios/XYAPP.git

一个索引库可以存放多个索引文件

2.创建组件本地代码库

为了方便理解,创建一个名为 XYBase的pod库

在终端中cd到某一路径下,执行命令

pod lib create XYBase

后,pod会询问pod库的属性,根据提示输入即可

3.组件项目开发

3.1 在~/XYBase/XYBase/Classes路径下放入源代码文件,cd到~/XYBase/Example路径下后执行pod install命令

3.2 在 XYBase.podspec 文件中编写pod文件路径,依赖的pod库

# pod文件子模块配置
s.subspec 'XYBaseModel' do |ybm|
   # 路径配置
   ybm.source_files = 'XYBase/Classes/XYBaseModel/*'
   # 资源文件配置
   ybm.resource = 'XYBase/Assets/文件名'
   # 头文件配置
   ybm.prefix_header_file = 'XYBase/Classes/XYBaseModel/头文件名.h'
   # 依赖配置
   ybm.dependency '私有pod库'
   ybm.dependency 'Masonry' #公开pod库
   ybm.dependency 'XYBase/XYBaseNet' #依赖其它子模块
end

当依赖私有pod库时,需要在podfile中导入该库的索引地址

source 'https://github.com/CocoaPods/Specs.git'
source '私有索引库git地址'

上述操作完毕后,在终端执行pod install命令

4.开发完毕后推送到远端

4.1 创建远端代码仓库

4.2 修改 XYBase.podspec 文件中的s.version,s.homepage= '远端代码仓库主页地址',s.source = { :git => '远端代码仓库', :tag => s.version.to_s }配置;并执行pod install命令

4.3 将本地代码推送到远程代码库,打tag后推送tag

4.4 cd到组件根路径,执行校验命令

pod spec lint XYBase.podspec --allow-warnings --use-libraries --sources='https://github.com/CocoaPods/Specs.git'

# --sources:如果依赖私有库,需要加上该私有库的索引路径,中间以 ‘,’ 逗号隔开
# example
pod spec lint XYBase.podspec --allow-warnings --use-libraries --sources='http://gitlab/ios/XYAPP.git, https://github.com/CocoaPods/Specs.git'

# 在后面可添加 --verbose 选项,可输出更详细的验证情况

如果校验失败,可检查version、tag、子模块依赖、文件路径等

4.5 推送代码到pod库,执行命令

# push 后 [本地索引库名称][组件podspec文件名称]
pod repo push XYAPP XYBase.podspec --allow-warnings --use-libraries --sources='https://github.com/CocoaPods/Specs.git'

# --sources:如果依赖私有库,需要加上该私有库的索引路径,中间以 ‘,’ 逗号隔开
# example
pod repo push XYAPP XYBase.podspec --allow-warnings --use-libraries --sources='http://gitlab/ios/XYAPP.git, https://github.com/CocoaPods/Specs.git'
# 在后面可添加 --verbose 选项,可输出更详细的验证情况

如果校验失败,可检查version、tag、子模块依赖、文件路径等

5.使用

5.1 在podfile中书写索引文件路径,并书写 pod命令

source 'https://github.com/CocoaPods/Specs.git'
source 'http://gitlab/ios/XYAPP.git'

pod 'XYBase'

5.2 执行pod install命令

6.资源文件引入

6.1 resource_bundle & resource_bundles

允许定义当前 Pod 库的资源包的名称和文件。用 hash 的形式来声明,key 是 bundle 的名称,value 是需要包括的文件的通配 patterns。
CocoaPods 官方推荐使用 resource_bundles,因为用 key-value 可以避免相同名称资源的名称冲突。
同时建议 bundle 的名称至少应该包括 Pod 库的名称,可以尽量减少同名冲突。

s.resource_bundle = { 'XYBase' => 'XYBase/Assets/*.{png,storyboard,xib,xcassets,mp4}'}
#或
s.resource_bundles = {
  'XYBase' => ['XYBase/Assets/*.{png,storyboard,xib,xcassets,mp4}']
}
# 'XYBase'对应在ipa包中生成XYBase.bundle文件
//此处baseBundle其实是mainBundle
NSBundle * baseBundle = [NSBundle bundleForClass:NSClassFromString(@"XYBase")];
//获取XYBase组件生成的bundle
NSBundle * assetBundle = [NSBundle bundleWithPath:[baseBundle pathForResource:@"XYBase" ofType:@"bundle"]];
//根据bundle获取图片;xib,sb,视频等同理
UIImage *img = [UIImage imageNamed:@"imageName" assetBundle compatibleWithTraitCollection:nil];

如果手动导入的第三方库就是使用的bundle文件,且其在内部的是通过mainBundle去获取ThirdFrameWork.bundle,此时如果使用resource_bundles配置路径,ThirdFrameWork.bundle则会被打包进XYBase.bundle中,则取不到所需的图片等资源文件,所以需要使用下面👇的方法

6.2 resource & resources

使用resources来指定资源,被指定的资源只会简单的被copy到目标工程中(mainBundle)。

s.resources = [
    'XYBase/Assets/ThirdFrameWork.bundle'
]
#ipa包的文件结构,即`mainBundle`的文件结构
.
├── AppIcon
├── Assets.car
├── Base.lproj
├── Info.plist
├── LaunchScreen.storyboardc
├── ThirdFrameWork.bundle
├── XYBase.bundle
│   ├── Assets.car
│   ├── Info.plist
│   ├── XYBaseView.nib
│   └── XYBaseVideo.mp4
├── _CodeSignature
│   └── CodeResources
├── embedded.mobileprovision
└── XYAPP

ps.其它一些命令

# 查看本地索引库
pod repo list
# 删除本地索引库
pod repo remove xx
# 本地校验pod配置
pod lib lint xx.podspec

相关文章

网友评论

      本文标题:CocoaPods组件化集成

      本文链接:https://www.haomeiwen.com/subject/ucqsuhtx.html