iOS APP Framework化
目标:iOSAPP Framework化。
方法:资源文件打包成bundle,其它逻辑实现封装成framework静态框架。
准备:
一个基本功能的APP

1,将资源文件打包生成bundle
目标文件:图片等资源文件以及画面XIB
建立工程:File-> New -> Project -> macOS -> Framework & Library -> Bundle

拷贝导入资源文件:

修改项目TARGETS的BaseSDK为iOS:

至此工程已经建立完毕,编译工程生成.bundle文件

右键单击.bundle->show in finder便可找到生成的bundle
2,逻辑实现封装成静态框架
目标文件:.m .h文件
建立工程:File-> New -> Project -> iOS -> Framework & Library -> Cocoa Touch Framework

拷贝导入资源文件:

添加BundleTools,用来获取我们生成的.bundle里的资源文件
BundleTools.h
#import
#define BUNDLE_NAME @"MySource"
@interface BundleTools : NSObject
+ (NSString *)getBundlePath: (NSString *) assetName;
+ (NSBundle *)getBundle;
@end
BundleTools.m
#import "BundleTools.h
@implementation BundleTools
+ (NSBundle *)getBundle{
return [NSBundle bundleWithPath: [[NSBundle mainBundle] pathForResource: BUNDLE_NAME ofType: @"bundle"]];
}
+ (NSString *)getBundlePath: (NSString *) assetName{
NSBundle *myBundle = [BundleTools getBundle];
if (myBundle && assetName) {
return[[myBundle resourcePath] stringByAppendingPathComponent: assetName];
}
return nil;
}
@end

修改我们导入的.m文件,添加init和initWithNibName方法使其初始化时候获取bundle里面的资源。
- (id)init{
NSBundle *myBundle = [BundleTools getBundle];
self = [super initWithNibName: [NSString stringWithUTF8String: object_getClassName(self)] bundle: myBundle];
if (self) {
}
return self;
}
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
}
return self;
}
至此工程已经建立完毕,编译工程生成.framework文件
右键单击.framework->show in finder便可找到生成的framework

3,建立测试工程

复制导入framework与bundle
工程TARGETS -> Embedded Binaries -> 添加framework

建立测试画面,使用framework功能

至此OS APP Framework化完毕,运行实现效果。



网友评论