用NSSetUncaughtExceptionHandler捕获程序异常

来源:互联网 发布:telnet文件linux 编辑:程序博客网 时间:2024/05/28 16:13
void UncaughtExceptionHandler(NSException *exception) {

NSArray *arr = [exception callStackSymbols];

NSString *reason = [exception reason];

NSString *name = [exception name];

NSString *urlStr = [NSString stringWithFormat:@"mailto://kobecn@gmail.com?subject=bug报告&body=感谢您的配合!<br><br><br>"

"错误详情:<br>%@<br>--------------------------<br>%@<br>---------------------<br>%@",

name,reason,[arr componentsJoinedByString:@"<br>"]];

NSURL *url = [NSURL URLWithString:[urlStr stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];

[[UIApplication sharedApplication] openURL:url];

}

然后在delegate文件里面

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions函数里面添加NSSetUncaughtExceptionHandler (&UncaughtExceptionHandler);


/////////---------------------------------------------------------------------->>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

//异常处理

void uncaughtExceptionHandler(NSException *exception) {

   NSLog(@"CRASH: %@", exception);

   NSLog(@"Stack Trace: %@", [exceptioncallStackSymbols]);

}


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

{

 

    NSSetUncaughtExceptionHandler(&uncaughtExceptionHandler);

    application.statusBarHidden =NO;

    NSLog(@"chengle");

    self.window = [[[UIWindowalloc] initWithFrame:[[UIScreenmainScreen] bounds]]autorelease];

    // Override point for customization after application launch.

    self.window.backgroundColor = [UIColorwhiteColor];

    [selfgoTabbarController:1];

    [self.windowmakeKeyAndVisible];

    return YES;

}