iOS开发:崩溃捕获

崩溃的捕获 需要在AppDelegate中注册并实现方法即可。


1:注册

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {

NSSetUncaughtExceptionHandler(handleExceptionAndTalk);

return YES;

}

2:实现handleExceptionAndTalk方法

void handleExceptionAndTalk(NSException *exception){

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

//保存异常信息

NSMutableDictionary *info = [NSMutableDictionary dictionary];

info[@"name"] = [exception name];                          // 异常名字

info[@"reason"] = [exception reason];                      // 异常描述(报错理由)

info[@"callStackSymbols"] = [exception callStackSymbols];  // 调用栈信息(错误来源于哪个方法)

//写入沙盒

NSString *path =[NSHomeDirectory() stringByAppendingString:@"/crash.plist"];

[info writeToFile:path atomically:YES];

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

NSMutableString *mailUrl = [NSMutableString string];

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

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

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

// 打开地址

NSString *mailPath = [mailUrl stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];

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

}

你可能感兴趣的:(iOS开发:崩溃捕获)