制作步骤
这部分资料很多,这里就不再赘述。
参考资料:
要点
如果需要调用 framework 中的分类(category)方法,需要在使用静态库的工程中配置 other linker flags 为 -Objc
默认 framework 是动态库,制作静态库需要设置 Mach-O Type 为 Static Library
想要在 framework 进行断点调试,可以在 framework 工程中创建一个新的 Target -> Single View
需要选择正确的指令集,真机 arm64,模拟器 x86_64
每一个需要暴露的 class 都需要在 Target Membership 中设置为 Public
工程中要使用 framework 中的资源,需要在工程中的 Build Phases -> Copy Bundle Resources 中添加 framework
1
2
3
4
5
6
7
8
9
10
11
12
13- (UIImage *)downloadImage {
NSBundle *bundle = [NSBundle bundleWithPath:[[NSBundle mainBundle] pathForResource:@"DownloadAndPrint" ofType:@"framework"]];
NSString* imagesPath = [bundle.resourcePath stringByAppendingPathComponent:@"images"];
NSLog(@"imagesPath:\%@", imagesPath);
NSBundle* imageBundle = [NSBundle bundleWithPath:imagesPath];
return [UIImage imageNamed:@"test" inBundle:imageBundle compatibleWithTraitCollection:nil];
}
iOS framework 合并和拆分多种架构
参考资料:
合并:
Target -> Build Settings -> Build Active Architecture Only(是否只编译当前架构) -> Debug 改为 NO(改为 NO,模拟器可以直接合成两种架构)
1 | ➜ ~ lipo -create /Users/acan/Library/Developer/Xcode/DerivedData/AFNTestFrameworking-dbzgzczpzorkzxakdyptgdkckcee/Build/Products/Debug-iphonesimulator/AFNTestFrameworking.framework/AFNTestFrameworking /Users/acan/Library/Developer/Xcode/DerivedData/AFNTestFrameworking-dbzgzczpzorkzxakdyptgdkckcee/Build/Products/Debug-iphoneos/AFNTestFrameworking.framework/AFNTestFrameworking -output /Users/acan/Library/Developer/Xcode/DerivedData/AFNTestFrameworking-dbzgzczpzorkzxakdyptgdkckcee/Build/Products/Debug-iphonesimulator/AFNTestFrameworking.framework/AFNAllFramework |
查看:
1 | ➜ ~ lipo -info /Users/acan/Library/Developer/Xcode/DerivedData/AFNTestFrameworking-dbzgzczpzorkzxakdyptgdkckcee/Build/Products/Debug-iphonesimulator/AFNTestFrameworking.framework/AFNAllFramework |
拆分:
1 | ➜ ~ lipo /Users/acan/Library/Developer/Xcode/DerivedData/AFNTestFrameworking-dbzgzczpzorkzxakdyptgdkckcee/Build/Products/Debug-iphonesimulator/AFNTestFrameworking.framework/AFNTestFrameworking -thin x86_64 -output /Users/acan/Library/Developer/Xcode/DerivedData/AFNTestFrameworking-dbzgzczpzorkzxakdyptgdkckcee/Build/Products/Debug-iphonesimulator/AFNTestFrameworking.framework/AFNSingleFrameworking |
查看
1 | Non-fat file: /Users/acan/Library/Developer/Xcode/DerivedData/AFNTestFrameworking-dbzgzczpzorkzxakdyptgdkckcee/Build/Products/Debug-iphonesimulator/AFNTestFrameworking.framework/AFNSingleFrameworking is architecture: x86_64 |
查看 framework 中包含的文件
使用 ar -t
命令,如:
1 | ➜ ~ ar -t /Users/acan/Library/Developer/Xcode/DerivedData/AFNTestFrameworking-dbzgzczpzorkzxakdyptgdkckcee/Build/Products/Debug-iphoneos/AFNTestFrameworking.framework/AFNTestFrameworking |
1 | ~ ar -t /Users/acan/Library/Developer/Xcode/DerivedData/DownloadAndPrint-esbkggakvotbghavvyrahlbwezwq/Build/Products/Debug-iphonesimulator/DownloadAndPrint.framework/DownloadAndPrint |
报错及解决办法
- 找不到头文件
创建了一个名为 AFNTestFrameworking 的静态 framework,里面包含一个 AFNTestManager 的类:
在 AFNTestFramework.h 类中,引入需要暴露出来的类:
编译的时候没有问题,但是将这个 framework 导入到测试项目时,报错:
解决办法:
- 动态链接找不到对应的库
1 | dyld: Library not loaded: @rpath/AFNetworking.framework/AFNetworking |
解决办法:
- 符号表冲突的问题
场景描述:我制作了一个 .framework 静态库,静态库引用了 AFNetworking,在我的 Demo 工程中也引用了 AFNetworking,报了以下错误:
1 | ld: 165 duplicate symbols for architecture x86_64 |
解决办法:
把 framework 中用到的第三方库的类更改名字
Step 1:修改类名
Step 2:根据报错提示,全局搜索,手动修改冲突的类名
1 | uplicate symbol _OBJC_CLASS_$__AFURLSessionTaskSwizzling in: |
如:
过程比较繁琐,但是有效。