Crashlytics doesn't work in iOS keyboard extension

孤者浪人 提交于 2019-12-06 15:37:25

A few minutes after posting this, it occcured to me to actually set a symbolic breakpoint on [NSURLSessionConfiguration backgroundSessionConfigurationWithIdentifier:] instead of on the methods I had tried in the past. The breakpoint was hit in Crashlytics code.

To fix this, I swizzled backgroundSessionConfigurationWithIdentifer: with a method that returns the default configuration. Implementation is below:

static Class URLSessionClass;

@implementation NSURLSessionConfiguration (FixCrashlyticsBug)

+ (void)load {
  static dispatch_once_t onceToken;
  dispatch_once(&onceToken, ^{
    URLSessionClass = object_getClass((id)self);
  });
}

+ (NSURLSessionConfiguration *)defaultSessionConfigurationWithIdentifier:(NSString *)__unused identifer {
  return [self defaultSessionConfiguration];
}

@end

@implementation CrashlyticsInterfaceManager

+ (void)startCrashlyticsFromExtension {
//Do the swizzle here instead of in load, so we don't do it in the container app as well
  static dispatch_once_t onceToken;
  dispatch_once(&onceToken, ^{
    SEL originalSelector = @selector(defaultSessionConfigurationWithIdentifier:);
    SEL swizzledSelector = @selector(backgroundSessionConfigurationWithIdentifier:);
    Class class = URLSessionClass;
    Method swizzledMethod = class_getClassMethod(class, swizzledSelector);
    Method originalMethod = class_getClassMethod(class, originalSelector);
    BOOL didAddMethod = class_addMethod(class,
                                        originalSelector,
                                        method_getImplementation(swizzledMethod),
                                        method_getTypeEncoding(swizzledMethod));
    if (didAddMethod) {
      class_replaceMethod(class,
                          swizzledSelector,
                          method_getImplementation(originalMethod),
                          method_getTypeEncoding(originalMethod));
    } else {
      method_exchangeImplementations(originalMethod, swizzledMethod);
    }
    [Crashlytics startWithAPIKey:@"MyAPIKey"];
  });
}

@end

Clashlytics is distinguished by BundleIdentidfier, it seems to work. Main app , App Extension is a different Bundle Identifier.

In the same Bundle Identifier and Keyboard App, you will create another new project. If you set the icon for the Keyboard App Extension to the new project , it would be better. Icon will be used in Clashlytics Web page. 

Install clashlytics to the newly created project, and complete. Now , it also works with the original Project. When you are finished , you can delete new project.

I worked in this way.

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