动态库加载过程

2017-07-14  本文已影响52人  碧玉小瑕

1,将framework引入工程中比如Dylib.framework

2,设置target--->Build Phases--->Link Binary with Libraries,添加framework

3,设置target--->Build Phases---->Copy Bindle Resource,

4,添加Copy Files,

如图:

5,设置Build Settings---->Linking----->Runpath Search Paths---->@executable_path

6,代码部分:

在使用的地方引入#import <Dylib/Dylib.h>

- (IBAction)bundleLoadAction:(id)sender {

NSLog(@"bundleLoadAction");

//NSString *documentsPath = [NSString stringWithFormat:@"%@/Documents/Dylib.framework",NSHomeDirectory()];

NSString*documentsPath = [NSStringstringWithFormat:@"%@/Dylib.framework",[[NSBundlemainBundle]bundlePath]];

[selfbundleLoadDylibWithPath:documentsPath];

}

- (void)bundleLoadDylibWithPath:(NSString*)path

{

_libPath= path;

NSError*err =nil;

NSBundle*bundle = [NSBundlebundleWithPath:path];

if([bundleloadAndReturnError:&err]) {

NSLog(@"bundle load framework success.");

}else{

NSLog(@"bundle load framework err:%@",err);

}

}

- (IBAction)trigerAction:(id)sender {

NSLog(@"trigerAction");

ClassrootClass =NSClassFromString(@"Person");

if(rootClass) {

idobject = [[rootClassalloc]init];

[(Person*)objectrun];

}

}

三个函数可看到效果。

上一篇下一篇

猜你喜欢

热点阅读