美文网首页
iOS 使用 use_frameworks 后 Bundle 的

iOS 使用 use_frameworks 后 Bundle 的

作者: 天空中的球 | 来源:发表于2020-08-11 20:13 被阅读0次
一、了解下 use_frameworks
  • 在Cocoapods 里使用 use_frameworks! 的话,是通过 frameworks 这个方式来管理 pod 的代码。否则则是 static libraries 方式来管理。
不使用use_frameworks ==> .a
使用use_frameworks ==> framework
  • Swift 项目 Cocoapods 默认 use_frameworks!
  • Objective-C 项目Cocoapods 默认 #use_frameworks!
  • 用 Cocoapods 导入Swift 框架 到 Swift项目和 Objective-C 项目都必须要 use_frameworks!
  • 用动态库,必须要在 Podfile 文件中添加 use_frameworks!
二、用了 use_frameworks Bundle 之后的影响
  • 如果不使用framework,Pod里的资源文件会被打成 bundle 放在mainbundle下面;
  • 如果使用framework,bundle 就会被分配在每个pod.framework下;
三、解决 项目用了 use_frameworks 之后 bundle 改变导致获取不到图片的问题

所以读取的方式,就需要兼容下啦

  • UIBundle 增加一个 分类,兼容下:
+ (instancetype)xx_bundleName:(NSString *)bundleName {
    NSURL *associateBundleURL = [[NSBundle mainBundle] URLForResource:@"Frameworks" withExtension:nil];
    associateBundleURL = [associateBundleURL URLByAppendingPathComponent:bundleName];
    associateBundleURL = [associateBundleURL URLByAppendingPathExtension:@"framework"];
    NSBundle *associateBunle = associateBundleURL?[NSBundle bundleWithURL:associateBundleURL]:[NSBundle mainBundle];
    associateBundleURL = [associateBunle URLForResource:bundleName withExtension:@"bundle"];
    NSBundle *bundle = associateBundleURL?[NSBundle bundleWithURL:associateBundleURL]:[NSBundle mainBundle];
    return bundle;
}
  • UIImage 直接读取
+ (instancetype)xx_imgWithName:(NSString *)name bundle:(NSString *)bundleName {
    NSBundle *resource_bundle = [NSBundle xx_bundleName:bundleName];
    UIImage *image = [UIImage imageNamed:name
                                    inBundle:resource_bundle
               compatibleWithTraitCollection:nil];
    return image;
    
}
以上 Bundle 问题,主要是发生在进行组件化或者打静态库的时候,资源文件的处理。

相关文章

网友评论

      本文标题:iOS 使用 use_frameworks 后 Bundle 的

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