在程序运行的那个方法里面写一个监听事件 NSSetUncaughtExceptionHandler(&UncaughtExceptionHandler);
//崩溃日志 然后实现监听的方法
voidUncaughtExceptionHandler(NSException*exception) {
NSArray*arr = [exceptioncallStackSymbols];//得到当前调用栈信息
NSString*reason = [exceptionreason];//非常重要,就是崩溃的原因
NSString*name = [exceptionname];//异常类型
NSLog(@"招用: %@ \n招用:原因: %@ \n招用:类型: %@", name, reason, arr);
}
获取到了崩溃的日子,如何发送给开发者呢,目前一般有以下两种方式:
1. 将崩溃信息持久化在本地,下次程序启动时,将崩溃信息作为日志发送给开发者。
通过邮件发送给开发者。 不过此种方式需要得到用户的许可,因为iOS不能后台发送短信或者邮件,会弹出发送邮件的界面,只有用户点击了发送才可发送。 不过,此种方式最符合苹果的以用户至上的原则。
发送邮件代码也很简单:
NSString *crashLogInfo = [NSString stringWithFormat:@”exception type : %@ \n crash reason : %@ \n call stack info : %@”, name, reason, arr];
NSString *urlStr = [NSString stringWithFormat:@”mailto://tianranwuwai@yeah.net?subject=bug报告&body=感谢您的配合!
”
“错误详情:%@”,
crashLogInfo];
NSURL *url = [NSURL URLWithString:[urlStr stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
[[UIApplication sharedApplication] openURL:url];