捕获异常

//捕获异常

NSSetUncaughtExceptionHandler(&UncaughtExceptionHandler);

oid UncaughtExceptionHandler(NSException *exception) {

/**

*  获取异常崩溃信息

*/

NSArray *callStack = [exception callStackSymbols];

NSString *reason = [exception reason];

NSString *name = [exception name];

NSString *content = [NSString stringWithFormat:@"========异常错误报告========\nname:%@\nreason:\n%@\ncallStackSymbols:\n%@",name,reason,[callStack componentsJoinedByString:@"\n"]];

//记录日志

NSString *exceptionPath = [APP_TMPPATH stringByAppendingPathComponent:[NSString stringWithFormat:@"%.0f.txt",[[NSDate date] timeIntervalSince1970] * 1000]];

if ([[NSFileManager defaultManager] fileExistsAtPath:exceptionPath]) {;

[[NSFileManager defaultManager] createFileAtPath:exceptionPath contents:nil attributes:nil];

}

[content writeToFile:exceptionPath atomically:YES encoding:NSUTF8StringEncoding error:nil];

//提示框

UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Exception" message:content delegate:nil cancelButtonTitle:nil otherButtonTitles:nil, nil];

[alert show];

/**

*  把异常崩溃信息发送至开发者邮件

*/

NSMutableString *mailUrl = [NSMutableString string];

[mailUrl appendString:@"[email protected]"];

[mailUrl appendString:@"?subject=程序异常崩溃,请配合发送异常报告,谢谢合作!"];

[mailUrl appendFormat:@"&body=%@", content];

// 打开地址

NSString *mailPath = [mailUrl stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];

[[UIApplication sharedApplication] openURL:[NSURL URLWithString:mailPath]];

}

你可能感兴趣的:(捕获异常)