iOS在线更新framework,使用NSBundle动态读取

时间:2023-03-09 17:05:20
iOS在线更新framework,使用NSBundle动态读取

官方文档:https://developer.apple.com/library/mac/documentation/Cocoa/Conceptual/LoadingCode/Tasks/LoadingBundles.html

framework制作:http://www.cocoachina.com/ios/20141126/10322.html

1.framework代码:framework一定要打包为动态库

@implementation TestView
-(id)initWithFrame:(CGRect)frame
{
if (self=[super initWithFrame:frame])
{
UIButton *btn=[[UIButton alloc] initWithFrame:CGRectMake(, , , )];
[btn setBackgroundColor:[UIColor redColor]];
[self addSubview:btn];
}
return self;
}
@end

2.将制作好的framework直接压缩成zip包FrameWorkOne.framework.zip,然后上传到服务器

3.在iOS程序中下载zip并解压缩

4.NSBundle load解压缩的framework动态直接代码

 NSString* bundlefile = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents/FrameWorkOne.framework"];
NSBundle *frameworkBundle = [NSBundle bundleWithPath:bundlefile];
if (frameworkBundle && [frameworkBundle load]) {
NSLog(@"bundle load framework success.");
}else {
NSLog(@"bundle load framework err"); return;
} Class pacteraClass = NSClassFromString(@"TestView");
if (!pacteraClass) {
NSLog(@"Unable to get TestDylib class"); return;
} UIView *v=[[pacteraClass alloc] initWithFrame:self.view.bounds];
[self.view addSubview:v];
[self.view sendSubviewToBack:v]; [frameworkBundle unload];