试着抓住整个应用程序

时间:2013-12-19 09:22:18

标签: ios objective-c error-handling try-catch

我想知道是否有可能试试整个应用程序?我的计划是这样做,然后在UIAlertView中显示任何错误,并带有一个“电子邮件开发者”按钮,预先填写一封电子邮件给我。

虽然不是一个完美的用户体验,但比应用程序更容易崩溃。

1 个答案:

答案 0 :(得分:1)

你可以做点好事。 'NSSetUncaughtExceptionHandler'。

你的applicationDidFinishLaunchingWithOptions中的

添加:

NSSetUncaughtExceptionHandler(&HandleException);

当然,将c函数添加到此类:

void HandleException(NSException *exception)
{
        int32_t exceptionCount = OSAtomicIncrement32(&UncaughtExceptionCount);
if (exceptionCount > UncaughtExceptionMaximum)
{
    return;
}

NSArray *callStack = [UncaughtExceptionHandler backtrace];
NSMutableDictionary *userInfo =
    [NSMutableDictionary dictionaryWithDictionary:[exception userInfo]];
[userInfo
    setObject:callStack
    forKey:UncaughtExceptionHandlerAddressesKey];

[[[[UncaughtExceptionHandler alloc] init] autorelease]
    performSelectorOnMainThread:@selector(handleException:)
    withObject:
        [NSException
            exceptionWithName:[exception name]
            reason:[exception reason]
            userInfo:userInfo]
    waitUntilDone:YES];
}

添加handleException方法以随时显示错误。

编辑: 您甚至可以看到堆栈跟踪:请参阅NSException的“callStackSymbols”。

相关问题