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

本文介绍如何在iOS应用中创建和使用动态加载的framework。主要内容包括实现TestView类的方法,将framework打包并上传到服务器,从服务器下载并解压,最后通过NSBundle加载并使用该framework。

官方文档: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];

 

转载于:https://www.cnblogs.com/bandy/p/4442137.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值