程序异常退出时发送邮件报告的代码

利用NSSetUncaughtExceptionHandler,当程序异常退出的时候,就给指定的邮箱发送邮件,邮件内容包括发生异常时候的详情。代码如下:

void UncaughtExceptionHandler(NSException *exception) {
NSArray *arr = [exception callStackSymbols];
NSString *reason = [exception reason];
NSString *name = [exception name];
NSString *urlStr = [NSString stringWithFormat:@"mailto://[email protected]?subject=bug报告&body=感谢您的配合!<br><br><br>"
"错误详情:<br>%@<br>--------------------------<br>%@<br>---------------------<br>%@", 
name,reason,[arrcomponentsJoinedByString:@"<br>"]];
NSURL *url = [NSURL URLWithString:[urlStrstringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
[[UIApplication sharedApplication] openURL:url];
}

   然后在delegate文件里面- (BOOL)application:(UIApplication *)applicationdidFinishLaunchingWithOptions:(NSDictionary*)launchOptions函数里面添加NSSetUncaughtExceptionHandler(&UncaughtExceptionHandler);

   这样就大功告成了。

你可能感兴趣的:(程序异常退出时发送邮件报告的代码)