
- (void)bugly
{
dispatch_async(dispatch_get_global_queue(0, 0), ^{
if (NSClassFromString(@"Bugly")) {
Class clazz = NSClassFromString(@"Bugly");
SEL sel = NSSelectorFromString(@"startWithAppId:config:");
IMP imp = [clazz methodForSelector:sel];
void (*IMP_startWithAppId)(id, SEL, NSString *, id) = (void *)imp;
NSDictionary *info = [[NSBundle mainBundle] infoDictionary];
Class buglyConfig = NSClassFromString(@"BuglyConfig");
id config = [[buglyConfig alloc]init];
[config setValue:@"NO" forKey:@"debugMode"];
[config setValue:[info objectForKey:(NSString *)kCFBundleIdentifierKey] forKey:@"channel"];
[config setValue:[info objectForKey:(NSString *)kCFBundleNameKey] forKey:@"version"];
IMP_startWithAppId(clazz, sel, BUGLY_KEY, config);
}
});
}

就像上面的示例代码一样动态方法调用 为什么不安全,它是用于对接Bugly的一段代码. 我们不需要在我们的APP中导入错误的类. 通过获取该类的IMP动态方法调用 为什么不安全,我们可以直接调用错误的初始化方法来达到低耦合的目的.

在处理某些第三方框架或服务时,建议此过程更加灵活.
本文来自电脑杂谈,转载请注明本文网址:
http://www.pc-fly.com/a/tongxinshuyu/article-259054-1.html
资金3方拖管
最后那一下手势萌死我了~
嘴硬是没用的