framework是一种优秀的资源打包方式,咱们平时看到的第三方发布的framework大部分都是静态库,苹果对iOS容许使用动态库,可是要利用动态库热更新,因为苹果的审核和签名技术,暂时仍是不行,内部使用仍是可行的git
# 头文件部分
#import <Foundation/Foundation.h>
@interface DynamicLlib : NSObject
- (void)doSomething;
@end
# 实现部分
#import "DynamicLlib.h"
@implementation DynamicLlib
- (void)doSomething{
NSLog(@"doSomething!");
}
@end
复制代码
2.1. 使用NSBundle加载动态库github
- (IBAction)loadFrameWorkByBundle:(id)sender {
//从服务器去下载而且存入Documents下(只要知道存哪里便可),事先要知道framework名字,而后去加载
NSString *frameworkPath = [NSString stringWithFormat:@"%@/Documents/DynamicLlib.framework",NSHomeDirectory()];
NSError *err = nil;
NSBundle *bundle = [NSBundle bundleWithPath:frameworkPath];
NSString *str = @"加载动态库失败!";
if ([bundle loadAndReturnError:&err]) {
NSLog(@"bundle load framework success.");
str = @"加载动态库成功!";
} else {
NSLog(@"bundle load framework err:%@",err);
}
}
复制代码
2.2. 使用dlopen加载动态库bash
// 动态库中真正的可执行代码为DynamicLlib.framework/DynamicLlib文件,所以使用dlopen时指定加载动态库的路径为DynamicLlib.framework/DynamicLlib
NSString *documentsPath = [NSString stringWithFormat:@"%@/Documents/DynamicLlib.framework/DynamicLlib",NSHomeDirectory()];
[self dlopenLoadDylibWithPath:documentsPath];
if (dlopen([path cStringUsingEncoding:NSUTF8StringEncoding], RTLD_NOW) == NULL) {
char *error = dlerror();
NSLog(@"dlopen error: %s", error);
} else {
NSLog(@"dlopen load framework success.");
}
复制代码
2.3. 调用动态库中的方法服务器
//调用framework的方法,利用runtime运行时
- (IBAction)callMethodOfFrameWork:(id)sender {
Class DynamicLlibClass = NSClassFromString(@"DynamicLlib");
if(DynamicLlibClass){
//事先要知道有什么方法在这个framework中
id object = [[DynamicLlibClass alloc] init];
//因为没有引入相关头文件故经过performSelector调用
[object performSelector:@selector(doSomething)];
}else {
NSLog(@"调用方法失败!");
}
}
复制代码
附:个人博客地址app