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文件了。
具体分析,下面将会介绍。