Crash Report 之第三方框架PLCrashReporter

Crash Report 的手机一方面可以直接从iPhone设备提取,也可以从iTunes上下载,然而这两种方式都不是很好操作,用户的设备不可能随时提供给你,用户也不一定会向iTunes上传crash report,那么最好的方式就是在app里面我们自己收集crash report,然后通过网络、邮件等方式回传过来。
下面介绍一款crash report 收集的第三方框架 PLCrashReporter
下载地址: https://www.plcrashreporter.org/download

解压后打开文件夹可以在 Source 文件下找到 iOS版本的demo,由于demo的版本过早,在iOS9下也许会运行不起来,需要稍作修改
demo中源码如下:

- (void)initViews
{
    _memoryWarningReached = NO;

    // Override point for customization after app launch

    [window addSubview:viewController.view];
    [window makeKeyAndVisible];
}


// this functions sends the crash data to the server
- (void)sendCrashReport
{
    [window addSubview:startupViewController.view];
    [startupViewController startCrashData];
}

可以修改如下:

- (void)initViews
{
    _memoryWarningReached = NO;

    // Override point for customization after app launch
//    window.rootViewController = [UIViewController new];

//  [window addSubview:viewController.view];
    window.rootViewController = viewController;
    [window makeKeyAndVisible];
}


// this functions sends the crash data to the server
- (void)sendCrashReport
{
//  [window addSubview:startupViewController.view];
    window.rootViewController = startupViewController;
    [startupViewController startCrashData];
}

demo中有将crash report基本信息进行了打印出来,并进行上传服务器,我们可以修改

- (void) handleCrashReport

方法,将crash report信息保存为 .plcrash文件或者.crash 文件,然后通过http上传给我们的服务器或者其它操作等,修改如下:

- (void) handleCrashReport {
    PLCrashReporter *crashReporter = [PLCrashReporter sharedReporter];
    NSData *crashData;
    NSError *error;

    // Try loading the crash report
    crashData = [crashReporter loadPendingCrashReportDataAndReturnError: &error];
    if (crashData == nil) {
        NSLog(@"Could not load crash report: %@", error);
        [crashReporter purgePendingCrashReport];
        return;
    }

    NSString* docPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) firstObject];
    NSString* filePath = [docPath stringByAppendingPathComponent:@"d.plcrash"];

    [crashData writeToFile:filePath atomically:YES];

    // We could send the report from here, but we'll just print out
    // some debugging info instead
    PLCrashReport *report = [[PLCrashReport alloc] initWithData: crashData error: &error];
    if (report == nil) {
        NSLog(@"Could not parse crash report");
        [crashReporter purgePendingCrashReport];
        return;
    }

    NSLog(@"Crashed on %@", report.systemInfo.timestamp);
    NSLog(@"Crashed with signal %@ (code %@, address=0x%" PRIx64 ")", report.signalInfo.name,
          report.signalInfo.code, report.signalInfo.address);

    NSString *humanText = [PLCrashReportTextFormatter stringValueForCrashReport:report withTextFormat:PLCrashReportTextFormatiOS];

    [[humanText dataUsingEncoding:NSUTF8StringEncoding] writeToFile:[docPath stringByAppendingPathComponent:@"d.crash"] atomically:YES];

    // Purge the report
    [crashReporter purgePendingCrashReport];


    return;
}

然后我们就可以来分析 .crash或者.plcrash文件了。
具体分析,下面将会介绍。

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值