動態庫載入過程

weixin_33866037發表於2017-07-14

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

2,設定target--->Build Phases--->Link Binary with Libraries,新增framework

3,設定target--->Build Phases---->Copy Bindle Resource,

4,新增Copy Files,

如圖:


1286371-6c61c476deaf0b20.png


1286371-e4cd484dc15f7346.png

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];

}

}


三個函式可看到效果。

相關文章