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(0, 0, 100, 100)];
        [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];

 

posted @ 2015-04-20 17:25  Ethan_村长  阅读(6505)  评论(3编辑  收藏  举报