Google Analytics SDK iOS10

只谈情不闲聊 提交于 2020-01-15 05:59:08

问题


I have installed Google Analytics from cocoa pod version 3.14

id<GAITracker> tracker =  [[GAI sharedInstance] trackerWithTrackingId:oneTrackId];

crash on iOS 10 in code line

NSString *user_id = [tracker get:kGAIUserId];

Error * Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '* -[GAITrackerModel valueForKey:]: attempt to retrieve a value for a nil key'


回答1:


Apple changed valueForKey: method behavior in iOS 10. Previously calling valueForKey: with argument nil lead to calling valueForUndefinedKey: with nil, and if this method was not overriden, it fails. But now it fails instantly without this call.

GAITrackerModel has overriden valueForUndefinedKey:, that returns nil regardless of input argument.

I can offer method swizzling with restoring previous behavior as a temporary solution (Google should fix that and this code is not production-ready, but until then):

#import <objc/runtime.h>

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



回答2:


We've released version 3.17 of the Google Analytics iOS SDK. It contains a number of improvements and bug fixes, including a fix for this issue.

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

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



来源:https://stackoverflow.com/questions/38850192/google-analytics-sdk-ios10

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!