iOS在線更新framework,使用NSBundle動態讀取

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

framework製做:http://www.cocoachina.com/ios/20141126/10322.htmlios

 

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,而後上傳到服務器app

3.在iOS程序中下載zip並解壓縮spa

4.NSBundle load解壓縮的framework動態直接代碼code

 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];
相關文章
相關標籤/搜索