Google Analytics SDK iOS10

我从cocoa pod版本3.14安装了Google Analytics

id tracker = [[GAI sharedInstance] trackerWithTrackingId:oneTrackId]; 

代码行中的iOS 10崩溃

 NSString *user_id = [tracker get:kGAIUserId]; 

错误*由于未捕获的exception’NSInvalidArgumentException’而终止应用程序,原因:’* – [GAITrackerModel valueForKey:]:尝试检索nil键的值’

Apple在iOS 10中更改了valueForKey:方法行为。以前调用valueForKey:使用参数nil导致调用valueForUndefinedKey:使用nil ,如果此方法未被覆盖,则失败。 但现在没有这个电话,它立即失败了。

GAITrackerModel具有覆盖valueForUndefinedKey: ,无论输入参数如何都返回nil。

我可以提供方法调整以恢复以前的行为作为临时解决方案(谷歌应该修复它,这个代码不是生产就绪的,但在那之前):

 #import  void SwizzleInstanceMethod(Class classToSwizzle, SEL origSEL, Class myClass, SEL newSEL) { Method methodToSwizzle = class_getInstanceMethod(classToSwizzle, origSEL); Method myMethod = class_getInstanceMethod(myClass, newSEL); class_replaceMethod(classToSwizzle, newSEL, method_getImplementation(methodToSwizzle), method_getTypeEncoding(methodToSwizzle)); class_replaceMethod(classToSwizzle, origSEL, method_getImplementation(myMethod), method_getTypeEncoding(myMethod)); } @interface FixGoogleSDKiOS10 : NSObject @end @implementation FixGoogleSDKiOS10 + (void)load { static dispatch_once_t onceToken; dispatch_once(&onceToken, ^{ SwizzleInstanceMethod([NSObject class], @selector(valueForKey:), [self class], @selector(yb_valueForKey:)); }); } - (nullable id)yb_valueForKey:(NSString *)key { if (!key) { return [self valueForUndefinedKey:key]; } return [self yb_valueForKey:key]; } @end 

我们发布了Google Analytics iOS SDK 3.17版。 它包含许多改进和错误修复,包括针对此问题的修复。

CocoaPod: https ://cocoapods.org/pods/GoogleAnalytics

SDK下载: https : //developers.google.com/analytics/devguides/collection/ios/v3/sdk-download