前言
近期在工作中与SDK 配合开发,由于之前没做过framework,为了能更好的配合设计,所以进行了简单的了解,以下就是简单的一个入门
概述
为了完成此次GitHub-testFrameWork,我们需要创建三个类型的文件.
打开xcode -> shift + command + N 进行创建
- 资源文件:
macOS
->Bundle
- framework:
iOS
->Cocoa Touch Framework
- demo项目
1.创建配置资源文件Bundle
- 创建
Bundle
WX20190705-003327@2x.png
- 配置
Base SDK
根据项目需要修改此次模式,macOS模式 -> iOS模式
![](https://img.haomeiwen.com/i14295309/75c5c3d92aaf5f88.png)
- 配置
COMBINE_HIDPI_IMAGES
COMBINE_HIDPI_IMAGES 修改为 NO,否则bundle中的图片格式别替换成就tiff格式
![](https://img.haomeiwen.com/i14295309/e3b9a4b4e2d99866.png)
- 配置
Skip Install
作为资源包,仅仅需要编译,无需安装相关的配置,设置Skip install为YES
![](https://img.haomeiwen.com/i14295309/0ddaf0387cc9a498.png)
-
添加所需资源
WX20190705-004507@2x.png
-
编译生成.bundle文件,
command + B
编译后生成相应文件
Debug-iphoneos : 手机-Debug模式
Debug-iphonesimulator : 模拟器-Debug模式
Release-iphoneos : 手机-Release模式
Release-iphonesimulator : 模拟器-Release模式
![](https://img.haomeiwen.com/i14295309/e7991685d61deefe.png)
2.创建配置framework
- 创建framework -
Cocoa Touch Framework
WX20190705-004806@2x.png
- 配置
Build Active Architecture Only
Build Active Architecture Only为NO的意思是当前打包的.framework支持所有的设备.否则打包时只能用当前版本的模拟器或真机运行
![](https://img.haomeiwen.com/i14295309/a1dbc41c26705981.png)
- 配置
Dead Code Stripping
编译选项优化
![](https://img.haomeiwen.com/i14295309/3c487f6f49ff17be.png)
- 配置
Mach-O Type
Xcode默认是动态库,需要配置成静态库(StaticLibrary)
![](https://img.haomeiwen.com/i14295309/373a0434fa789861.png)
- 配置
iOS Deployment Target
最低支持版本
![](https://img.haomeiwen.com/i14295309/3353c7471225d23c.png)
-
删除自带的头文件
WX20190705-010122@2x.png
-
创建生成所需文件
创建了3个类作为例子
// 模型
Preson : NSObject
// 类方法
+ (void)eat;
// 控制器
TestViewController : UIViewController
// 代理
@property (nonatomic, weak) id<TestViewControllerDelegate> delegate;
// 私有类,仅在framework中可以调用
Private : NSObject
根据下图设置公开或不公开
![](https://img.haomeiwen.com/i14295309/fb4b8ecc143798c0.png)
- 将
Bundle
整合进framework
打开刚刚的Bundle文件,command + B 编译后
Show in Finder 找到对应的文件
![](https://img.haomeiwen.com/i14295309/3df13c93fb9e9605.png)
将.bundle文件添加进framework
![](https://img.haomeiwen.com/i14295309/cc3f2c0297c9f90f.png)
- 在framework中使用.bundle的资源
// 获取plist文件 进行打印
NSString *bundlePath = [[NSBundle mainBundle] pathForResource:@"testFrameWork.framework/TestLibrary" ofType:@"bundle"];
NSBundle *testBundle = [NSBundle bundleWithPath:bundlePath];
NSString *p = [testBundle pathForResource:@"test_Plist" ofType:@"plist"];
NSDictionary *dic = [NSDictionary dictionaryWithContentsOfFile:p];
NSLog(@"\n-----------\ntestFrameWork->Preson读取到的plist\ntest_Plist:%@\n-------------",dic);
// 获取图片路径
NSString *bundlePath = [[NSBundle mainBundle] pathForResource:@"testFrameWork.framework/TestLibrary" ofType:@"bundle"];
NSBundle *testBundle = [NSBundle bundleWithPath:bundlePath];
NSString *path = [testBundle pathForResource:@"test_Image@2x" ofType:@"png"];
// 设置图片
imageView.image = [UIImage imageWithContentsOfFile:path];
- 配置生成
framework
设置模式,此次以Release模式为标准设置
红色框住内容,均设置为 Release
![](https://img.haomeiwen.com/i14295309/2647807560d15870.png)
分别设置手机与模拟器方式编译一次framework (Command + B)
![](https://img.haomeiwen.com/i14295309/bb2d1a70d3721d8b.png)
![](https://img.haomeiwen.com/i14295309/8685788a501bc5e4.png)
编译完成,找到对应的framework后,将两种模式合并,这样我们的生成的framework可以在手机和模拟器两种模式下使用
![](https://img.haomeiwen.com/i14295309/bfcde3f237d1a340.png)
![](https://img.haomeiwen.com/i14295309/b6250d0cf212a84b.png)
![](https://img.haomeiwen.com/i14295309/b7434b8d76baf9de.png)
进行合并
sudo lipo -create
![](https://img.haomeiwen.com/i14295309/9376ff6e08f74028.png)
![](https://img.haomeiwen.com/i14295309/9d8d31801fa527d7.png)
设置输出路径
-output
![](https://img.haomeiwen.com/i14295309/cfc9ed4478bf2522.png)
![](https://img.haomeiwen.com/i14295309/9d0eca7191a42325.png)
使用
lipo -info
查看是否成功
![](https://img.haomeiwen.com/i14295309/6261a19be51861d3.png)
替换文件,项目使时,记得拷贝我们替换过的
framework
![](https://img.haomeiwen.com/i14295309/19432673729ec8ae.png)
3.整合项目及使用
-
整合配置
WX20190705-160300@2x.png
WX20190705-112651@2x.png
-
framework
使用
// 导入头文件
#import <testFrameWork/Preson.h>
#import <testFrameWork/TestViewController.h>
// 调用类方法
[Preson eat];
-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
// 使用testVC
TestViewController *tVC = [[TestViewController alloc] init];
tVC.delegate = self;
[self.navigationController pushViewController:tVC animated:YES];
}
#pragma mark Delegate
-(void)clickTestViewController{
NSLog(@"TestViewController-----Delegate");
}
网友评论