動態庫的調用方式

一. Embedded Binaries 方式
1. 把 xx.framework 添加到 Embedded Binaries 中;
2. 保證Build Setting - Runpath Search Paths 中設置路徑,例如:@executable_path/Frameworks(xcode 一般會自動設置好);
3. 代碼中直接使用 xx.framework 頭文件中提供的類即可;


二. NSBundle 加載方式
NSBoundle *frameworkBundle = [NSBundle bundleWithPath:libPath];
if (frameworkBundle && [frameworkBundel load]) {
Class myclass = NSClassFromString(@"testClass");
NSObject *obj = [[myclass alloc] init];
[obj performSelector:@selector(test) withObject:self withObject:frameworkBundle];
}


三. dlopen 加載方式
void* lib_handle = dlopen([libPath cStringUsingEncoding:NSUTF8StringEncoding], RTLD_LOCAL);
if (!lib_handle) {
    NSLog(@"Unable to open library: %s\n", dlerror());
    return;
}

if (dlclose(lib_handle) != 0) {
    NSLog(@"Unable to close library: %s\n",dlerror());
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章