PLCrashReporter 是開源的,官網地址:https://www.plcrashreporter.org/html
把 CrashReporter.framework 引入集成到項目中,ios
implementation AppDelegate -(void)handleCrashReport { PLCrashReporter *crashReporter = [PLCrashReporter sharedReporter]; NSData *crashData; NSError *error; crashData = [crashReporter loadPendingCrashReportDataAndReturnError:&error]; if (crashData == nil) { NSLog(@"Could not load crash report: %@", error); [crashReporter purgePendingCrashReport]; return; } /* CrashData 能夠直接上傳到服務器上,下面的代碼是保存到 Document 中 */ NSArray *docPathArray = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); NSString *docPath = [docPathArray firstObject]; NSString *path = [docPath stringByAppendingString:@"/crashdata.crash"]; [crashData writeToFile:path atomically:YES]; PLCrashReport *report = [[PLCrashReport alloc] initWithData:crashData error:&error]; if (report == nil) { NSLog(@"Could not parse crash report: %@", error); [crashReporter purgePendingCrashReport]; return; } /* CrashData 還須要用 PLCrashReporter 的工具 crashutil 解析,也能夠直接保存成字符串*/ NSString *humanReadable = [PLCrashReportTextFormatter stringValueForCrashReport:report withTextFormat:PLCrashReportTextFormatiOS]; NSLog(@"Report: %@", humanReadable); } - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { PLCrashReporter *crashReporter = [PLCrashReporter sharedReporter]; NSError *error; if ([crashReporter hasPendingCrashReport]) { [self handleCrashReport]; } if (![crashReporter enableCrashReporterAndReturnError:&error]) { NSLog(@"Warning: Could not enable crash reporter: %@", error); } }
能夠用 PLCrashReporter 收集到 crashData,而後用 PLCrashReporter 的工具轉換爲正常的 .crash 文件,在 Terminal 中的命令以下(./ 是必需要帶的,哪怕已經 cd 到當前文件夾):服務器
./plcrashutil convert --format=iphone example_report.plcrash > app.crash
也能夠直接用 [PLCrashReportTextFormatter stringValueForCrashReport:report withTextFormat:PLCrashReportTextFormatiOS]
生成的字符串保存爲 .crash 文件。app
當得到到 .crash 日誌後,就須要分析而後定位到具體的哪一個文件的哪一行代碼了,使用 symbolicatecrash 便可,這裏有一篇教程:使用 symbolicatecrash 分析 .crash 文件iphone
export DEVELOPER_DIR="/Applications/Xcode.app/Contents/Developer"工具
./symbolicatecrash report.crash AppName.app.dSYM > 1001.crashatom