这可能无法完全回答您的问题,只需将我所知道的和我的猜测发短信即可。
首先,动态框架在你使用之前不会被加载到你的应用进程中,例如iOS模拟器设备中可用的目录下的框架。
> cd /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/Library/CoreSimulator/Profiles/Runtimes/iOS.simruntime/Contents/Resources/RuntimeRoot/System/Library/Frameworks
> # Now there are plenty of frameworks here.
> file AdSupport.framework/AdSupport
Mach-O 64-bit dynamically linked shared library x86_64
如何使用它?TLDR,[ASIdentifierManager sharedManager]
在您的应用程序中的任何地方调用它,首先链接到框架并成功编译它,当然。
第二NSClassFromString()
,直接使用和在任何地方调用有什么区别[ASIdentifierManager sharedManager]
?
对于前一种情况,您的应用程序不会加载AdSupport
框架包,因为当操作系统内核加载您的程序时,您的可执行程序中没有命名符号ASIdentifierManager
,可以通过打印您的应用程序主包路径并找到应用程序可执行文件来证明,尝试nm <path/to/executable_app> | grep "ASIdentifierManager"
,因为你没有使用它,所以在结果中找不到任何东西。
对于后一种,尝试在可执行程序中grep相同的符号,就可以了。
注意:不是操作系统内核按nm
结果列表加载框架,而是内核加载包含符号的框架,请查看有关动态加载器dyld的更多信息。
第三,NSClassFromString
只检查加载的类,如果AdSupport
框架没有加载,则返回nil,而不尝试加载包含目标类的框架。
第四AdSupport
,在您粘贴更多关于项目中 IDFA 和框架使用的上下文之前,不可能回忆起 iOS 10/11 和 iOS 12 之间的区别。这是我的一个猜测,一些依赖库在早期版本中使用该AdSupport
框架,但 iOS 12,您必须尝试在 iOS 11 和 iOS 12 之间转储符号列表并比较结果。
第五,我不确定你想要什么,也许你试图避免AdSupport
显式导入框架,如何NSBundle
通过框架路径初始化一个并调用-(BOOL)load
类NSBundle
,然后你可以Class
使用NSClassFromString
.
更新:
NSString *strFrameworkPath = nil;
#if TARGET_OS_SIMULATOR
strFrameworkPath = [[NSProcessInfo processInfo] environment][@"DYLD_FALLBACK_FRAMEWORK_PATH"];
#else
// Assume that the AdSupport and Foundation framework are in the same directory.
strFrameworkPath = [NSBundle bundleForClass:NSPredicate.class].bundlePath;
strFrameworkPath = [strFrameworkPath stringByDeletingLastPathComponent];
#endif
strFrameworkPath = [strFrameworkPath stringByAppendingPathComponent:@"AdSupport.framework"];
NSAssert([[NSFileManager defaultManager] fileExistsAtPath:strFrameworkPath], @"Invalid framework bundle path!");
NSBundle *bundle = [NSBundle bundleWithPath:strFrameworkPath];
if (!bundle.isLoaded) {
NSError *error = nil;
if (![bundle loadAndReturnError:&error]) {
DDLogError(@"Load framework bundle %@ with error %@", bundle, error);
}
}
DDLogDebug(@"bundle: %@", bundle.bundlePath);
DDLogDebug(@"class: %@", NSClassFromString(@"ASIdentifierManager"));
您可能需要为产品增强各种设备的兼容性,更多NSBundle
使用方法请查看官方文档。