第45月第28天 加载so库 iOS打包framework
1.
- (void)runtimePlay{ // 获取音乐资源路径 NSString *path = [[NSBundle mainBundle] pathForResource:@"rain" ofType:@"mp3"]; // 加载库到当前运行程序 void *lib = dlopen("/System/Library/Frameworks/AVFoundation.framework/AVFoundation", RTLD_LAZY); if (lib) { // 获取类名称 Class playerClass = NSClassFromString(@"AVAudioPlayer"); // 获取函数方法 SEL selector = NSSelectorFromString(@"initWithData:error:"); // 调用实例化对象方法 _runtime_Player = [[playerClass alloc] performSelector:selector withObject:[NSData dataWithContentsOfFile:path] withObject:nil]; // 获取函数方法 selector = NSSelectorFromString(@"play"); // 调用播放方法 [_runtime_Player performSelector:selector]; NSLog(@"动态加载播放"); }
https://www.jianshu.com/p/f931ff3b5d21
#include <unistd.h> #include <stdio.h> #include <stdlib.h> #include <sys/types.h> #include <dlfcn.h> /* 必须加这个头文件 */ #include <assert.h> int main(int argc, char *argv[]) { printf("come in\n"); void *handler = dlopen("libtest.so", RTLD_NOW);
printf("dlopen - %sn", dlerror());
assert(handler != NULL); void (*pTest)(int); pTest = (void (*)(int))dlsym(handler, "add"); (*pTest)(10); dlclose(handler); printf("go out\n"); return 0; }
编译so库
##### 在模拟器下编译
xcrun -sdk iphonesimulator clang -o libtest.so -shared -fPIC test.c
#在真机下编译
xcrun -sdk iphoneos clang -o libtest.so -shared -fPIC test.c
//gcc -o libtest.so -shared -fPIC test.c
#include <stdio.h> #ifdef __cplusplus extern "C"{ #endif void add(int num) { printf("*****************************************\n"); printf("This is ok, the number is okay. %d\n", num); printf("*****************************************\n"); } #ifdef __cplusplus } #endif
https://blog.csdn.net/skwaityou/article/details/9098297
2.iOS打包framework
设置bitcode
开启bitcode的前提,第三方库,支持bitcode编译才行。
TARGETS->项目->Build Settings 搜索 other c flags 添加-fembed-bitcode