首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >Firebase Crashlytics MacOS -在AppKit下所有崩溃

Firebase Crashlytics MacOS -在AppKit下所有崩溃
EN

Stack Overflow用户
提问于 2021-05-11 17:03:10
回答 1查看 89关注 0票数 0

我有一个移植到MacOS的iOS应用程序。该应用程序使用Firebase for Crashlytics。到目前为止,通过创建一个单独的Mac目标和为该目标创建单独的Firebase项目,我成功地配置好了一切。问题是,我在MacOS项目的控制台中看到的崩溃都在"AppKit“下。示例:

代码语言:javascript
复制
AppKit | -[NSApplication _crashOnException:] + 106

信息不是很丰富,是不是...现在,如果我检查崩溃,然后转到“Keys”,我仍然可以得到crashes异常:

代码语言:javascript
复制
crash_info_entry_0 | Crashing on exception: *** -[__NSCFCalendar rangeOfUnit:startDate:interval:forDate:]: date cannot be nil

但是,所有不同的崩溃都被分组到那个AppKit crash下,所以它不是很有帮助。

我意识到这个问题是由于AppKit在默认情况下捕获MacOS上的所有异常的默认行为造成的。有没有更好的方法来为MacOS设置Crashlytics,以便获得更细粒度的报告,就像在iOS和其他平台上一样?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-06-02 19:42:20

经过大量的研究,我发现这个问题没有完美的解决方案。我尝试重写NSApplication并将其设置为NSPrincipalClass,甚至实现了Sentry -没有成功。但是我找到了一种使用方法swizzling和FIRExceptionModel绕过AppKit的方法。

注意:首先,为了让Firebase Crashlytics在MacOS上工作,你需要在你的AppDelegate的didFinishLaunchingWithOptions中包含以下内容:

代码语言:javascript
复制
NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
[userDefaults registerDefaults:@{@"NSApplicationCrashOnExceptions" : @"YES"}];

然后,您需要创建一个NSApplication类别并混合使用_crashOnException方法:

代码语言:javascript
复制
#import <objc/runtime.h>
#import "NSApplication+CrashReport.h"
#import <FIRCrashlytics.h>

@implementation NSApplication (CrashReport)

+(void)load {
    static dispatch_once_t once_token;
    dispatch_once(&once_token,  ^{
        SEL crashOnExceptionSelector = @selector(_crashOnException:); // Ignore 'Undeclared selector' warning.
        SEL crashOnExceptionReporterSelector = @selector(reported__crashOnException:);
        Method originalMethod = class_getInstanceMethod(self, crashOnExceptionSelector);
        Method extendedMethod = class_getInstanceMethod(self, crashOnExceptionReporterSelector);
        method_exchangeImplementations(originalMethod, extendedMethod);
    });
}

- (void)reported__crashOnException:(NSException*)exception {
    NSArray<NSString*> *stacktrace = [exception callStackSymbols];
    [[FIRCrashlytics crashlytics]setCustomValue:stacktrace forKey:@"mac_os_stacktrace"];
    FIRExceptionModel *errorModel = [FIRExceptionModel exceptionModelWithName:exception.name reason:exception.reason];
    // The below stacktrace is hardcoded as an example, in an actual solution you should parse the stacktrace array entries.
    errorModel.stackTrace = @[
        [FIRStackFrame stackFrameWithSymbol:@"This stacktrace is fabricated as a proof of concept" file:@"Hello from Serge" line:2021],
        [FIRStackFrame stackFrameWithSymbol:@"__exceptionPreprocess" file:@"CoreFoundation" line:250],
        [FIRStackFrame stackFrameWithSymbol:@"objc_exception_throw" file:@"libobjc.A.dylib" line:48],
        [FIRStackFrame stackFrameWithSymbol:@"-[__NSCFCalendar rangeOfUnit:startDate:interval:forDate:]" file:@"CoreFoundation" line:453]
    ];
    // Note: ExceptionModel will always be reported as a non-fatal.
    [[FIRCrashlytics crashlytics] recordExceptionModel:errorModel];
    [self reported__crashOnException:exception];
}

@end

这段代码的要点是:https://gist.github.com/sc941737/c0c4542401ce203142c93ddc9b05eb1f

这意味着异常不会被报告为崩溃,而是非致命的。因此,我建议设置一个额外的自定义密钥,以便更容易地过滤非致命的崩溃。有关详细信息,请参阅Firebase文档:https://firebase.google.com/docs/crashlytics/customize-crash-reports?platform=ios

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/67483674

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档