捕捉異常類頭文件.h文件
///抓捕異常處理
void uncaughtExceptionHandler(NSException *exception);
捕捉異常實現文件.m文件
///抓捕異常處理
void uncaughtExceptionHandler(NSException *exception)
{
//手機型號
NSString *deviceName = [DeviceTool getPhoneModel];
//手機系統版本
NSString *deviceSystemVersion = [DeviceTool getSystemVersion];
//App 版本號
NSString *appVersion = [DeviceTool getAppVersion];
// 異常的堆棧信息
NSArray *stackArray = [exception callStackSymbols];
// 出現異常的原因
NSString *reason = [exception reason];
// 異常名稱
NSString *name = [exception name];
NSString *exceptionInfo = [NSString stringWithFormat:@"Device name:%@ SystemVersion:%@ AppVersion:%@ \nException reason:%@\nException name:%@\nException stack:%@",deviceName,deviceSystemVersion,appVersion,name, reason, stackArray];
NSMutableArray *tmpArr = [NSMutableArray arrayWithArray:stackArray];
[tmpArr insertObject:reason atIndex:0];
//保存到本地 -- 當然你可以在下次啟動的時候,上傳這個log
NSLog(@"錯誤日志路徑%@",Crash_Error_Path);
[exceptionInfo writeToFile:Crash_Error_Path atomically:YES encoding:NSUTF8StringEncoding error:nil];
//在這里發送網絡請求發送錯誤日志字符串,從服務器后台就可以看到錯誤日志
[self requestServerError];
}
//記得在應用程序啟動時初始化 在(appDelegate.m)
/*抓捕異常的處理*/
NSSetUncaughtExceptionHandler(&uncaughtExceptionHandler);
