iOS开发技巧iOS

iOS 静态库 和 bundle

2022-04-14  本文已影响0人  若水water

2、公司在开发项目时的核心代码

看一下 framework 和 Static Library 的区别
对外部代码依赖库的区别
运行环境的区别
如何选择两种库
framework 的结构
image.png

制作 .framework 静态库(闭源库)

image.png image.png image.png image.png image.png image.png image.png
创建bundle 文件
在framework 中添加bundle 资源
image.png image.png image.png image.png image.png image.png

此处我替换的是模拟器路径,所以将替换后的framework拿出来就行了,也可以测试一下 合并后支持的架构

image.png image.png
在项目工程中导入framework
image.png image.png image.png image.png image.png
    NSString *bundlePath = [[NSBundle mainBundle] pathForResource:@"LHNetwork.framework/HLNetworkSource" ofType:@"bundle"];
    
    NSLog(@"bundlePath = %@",bundlePath);
    
    NSBundle *bundle = [NSBundle bundleWithPath:bundlePath];
    NSString *pic1 = [bundle pathForResource:@"bill_head_bg@2x" ofType:@"png"];
    
    NSLog(@"pic1 = %@",pic1);
    
    UIImage *image = [UIImage imageWithContentsOfFile:pic1];
    
    NSLog(@"image = %@",image);
image.png

那么在工程和 framework中就可以用以下方式使用bundle中资源

    NSString *bundlePath = [[NSBundle mainBundle] pathForResource:@"HLNetworkSource" ofType:@"bundle"];

    NSLog(@"bundlePath = %@",bundlePath);

    NSBundle *bundle = [NSBundle bundleWithPath:bundlePath];

    NSString *imagePath = [bundle pathForResource:@"bill_head_bg@2x" ofType:@"png"];

    NSLog(@"imagePath = %@",imagePath);

    UIImage *image = [UIImage imageWithContentsOfFile:imagePath];

    NSLog(@"image = %@",image);

    imageView.image = image;
另一种将framework 导入工程的方式

直接将xxx.framework SDK拖入工程不太方便测试,在工程中看不到源代码,我们可以把framework的工程文件导入 ,就是创建的framework工程

image.png

至于导入的方式,可以参考我的另一篇文章 多工程联编
这样很方便的修改和调试。

注意

有的博客说,在上架App Store的时候,会有报错。可能需要把info.plist文件中的Excutable file删除,大家可以试一下,我没有实际操作。

补充说明,设备CPU架构
image.png

参考文章:
https://blog.csdn.net/jingcheng345413/article/details/54969324
https://www.cnblogs.com/mtystar/p/6082363.html

上一篇 下一篇

猜你喜欢

热点阅读