NSSetUncaughtExceptionHandler在iPhone上无法捕获所有错误

9

我正在使用来自http://code.google.com/p/google-toolbox-for-mac的GTMStackTrace。

我需要一种方法来测试最终用户在应用程序崩溃时向我发送错误。我知道如何将数据发送到我的网站,但问题是如何捕获所有未处理的错误。

我有这段代码:

void exceptionHandler(NSException *exception) {
    NSLog(@"%@", [exception reason]);
    NSLog(@"%@", [exception userInfo]);
    NSLog(@"%@", GTMStackTraceFromException(exception));

    UIAlertView *alert = [[UIAlertView alloc]
                          initWithTitle:NSLocalizedString(@"Error unexpected",@"Info: Can't save record")
                          message:GTMStackTraceFromException(exception) delegate:nil 
                          cancelButtonTitle:NSLocalizedString(@"Ok",@"Button: Ok") otherButtonTitles:nil];
    [alert show];
    [alert release];    
}

int main(int argc, char *argv[]) {
    //For crash report..
    NSSetUncaughtExceptionHandler(&exceptionHandler);
    //Normal code...
    NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
    int retVal = UIApplicationMain(argc, argv, nil, nil);
    [pool release];
    return retVal;
}

然而,该应用程序无法捕获许多错误,例如不良版本发布、BAD ACCES等,导致应用程序消失。我有两个问题不清楚为什么会发生,最终用户也不知道该说什么。
例如,重复发布相同的变量未被捕获。
那么,我如何获取所有这些烦人的错误,以便最终用户可以简单地向我发送崩溃报告?

还有,是否可以获取带有错误行的完整堆栈跟踪? - mamcx
您是否能够让UIAlertView弹出来?我无法操作,请问有什么建议吗? - Mark
2个回答

20

EXC_BAD_ACCESS并不会生成一个异常,而是生成一个信号(SIGSEGV)。要捕获它,你需要一个信号处理器。Christopher Atlan写了一篇很好的解释关于如何处理这两种崩溃。请确保阅读第一部分第二部分


1

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接