背景
1.在App上線時(shí)我們沒(méi)有辦法log手機(jī)的奔潰日志姥卢,現(xiàn)在網(wǎng)上有很多第三方收集奔潰日志的方法,常用的例如Crashlytics
2.但我們也需要知道如何使用系統(tǒng)框架收集crash日志
一缴允、在App崩潰時(shí)收集崩潰信息
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after application launch.
// 1.添加Crash崩潰函數(shù)監(jiān)聽(tīng)
NSSetUncaughtExceptionHandler (&UncaughtExceptionHandler);
return YES;
}
// 當(dāng)程序Crash時(shí)掉用此函數(shù)
void UncaughtExceptionHandler(NSException *exception) {
// 1.異常類型
NSString *name = [exception name];
// 2.非常重要,就是崩潰的原因
NSString *reason = [exception reason];
// 3.得到當(dāng)前調(diào)用棧信息
NSArray *logArray = [exception callStackSymbols];
// 4.崩潰日志
NSString *crashLogInfo = [NSString stringWithFormat:@"exception type : %@ \n crash reason : %@ \n call stack info : %@", name, reason, logArray];
// 5.可以緩存寫(xiě)入到本地,下次程序開(kāi)啟時(shí)發(fā)送的遠(yuǎn)端服務(wù)器
// 再次我設(shè)置崩潰信息發(fā)送至郵箱
NSString *urlStr = [NSString stringWithFormat:@"mailto://siming_zhu@163.com?subject=bug報(bào)告&body=感謝您的配合!,錯(cuò)誤詳情:%@", crashLogInfo];
NSURL *url = [NSURL URLWithString:[urlStr stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
[[UIApplication sharedApplication] openURL:url];
}
更多分享:
Github:https://github.com/zhusiming/
GitBook:https://zhusiming.gitbooks.io/smbook/