iOS 创建bundle (xcode 13)

bundle 资源包 是静态的,不参与项目的编译,所以不占包的大小,如果bundle里面要包含xib,那么需要创建工程的形式,将xib 序列化为静态nib。bundle 里面是不能加入二进制文件的。

  • command +shift+N 创建bundle 工程
image.png
  • 设置属性,buildSettings -》 Base SDK 设置为 iOS
image.png
  • Build Active Architecture Only 设置为YES(只编译当前环境,如果每个环境都编译一遍会使包变大)
image.png
  • Installation Directiotory 删除后面的路径(不安装相关配置,也不需要安装路径)
image.png
  • iOS Deployment Target 设置一个最低兼容版本,这里设置的9.0
image.png
  • Skip Install 设置为NO(不需要安装相关配置)
image.png
  • Strip Debug Symbols During Copy 中"Release"模式设置为 "YES"
image.png
  • COMBINE_HIDPI_IMAGES 设置为NO(不然图片会是tiff格式)
image.png
  • 下面导入图片资源
    在该项目下创建了 两个文件夹,一个放置图片资源,一个放xib 文件。 将图片直接拖入,xibs 文件夹下直接创建文件,copy bundle resources 就会自动导入。
image.png
  • 编译 分为debug 下,真机和模拟器; release 下 真机和模拟器(debug 模式下用于测试,release模式用于发版)

分别选择真机模式,和模拟器模式进行编译,编译通过后,command + ,
点击该路径,找到bundle文件

image.png
image.png
  • 选择模拟器或者真机下的bundle 导入项目,如果要上架App Store,那么就要选择真机下的bundle(release), 我这里是导入了静态库 framework,直接拖入工程,copy bundle resources 就会自动导入
image.png
  • 使用
    NSString *bundlePath = [[NSBundle mainBundle] pathForResource:@"HLNetworkSource" ofType:@"bundle"];
    NSBundle *bundle = [NSBundle bundleWithPath:bundlePath];
    NSString *imagePath = [bundle pathForResource:@"bill_head_bg" ofType:@"png"];
    NSData *data = [NSData dataWithContentsOfFile:imagePath];
    UIImage *image = [UIImage imageWithData:data];

如果要打包上线,可能会有些问题,再针对不同的问题去解决就行。我还没有试,如果有问题,望指出。

你可能感兴趣的:(iOS 创建bundle (xcode 13))