iOS - UncaughtExceptions全局异常处理程序不允许应用程序退出

Mar*_*ark 2 crash-reports ios4 ios

我正在尝试将Matt Gallagher的全局异常处理程序添加到我的一个项目中.运行他的示例项目位于:

http://cocoawithlove.com/2010/05/handling-unhandled-exceptions-and.html

我遇到一个问题,我按下退出并且应用程序没有退出.它只是让我回到应用程序.我尝试使用kill()调用来杀死应用程序,但无法让应用程序退出.

来自alertview的回调似乎只处理Continue情况,并且不处理强制应用程序退出.

- (void)alertView:(UIAlertView *)anAlertView clickedButtonAtIndex:(NSInteger)anIndex
{
   if (anIndex == 0)
   {
        dismissed = YES;
   }
}
Run Code Online (Sandbox Code Playgroud)

我知道应用程序本质上不能自行退出,但在这种情况下,如果应用程序崩溃,我希望用户按下退出按钮并让应用程序退出.

谢谢!

Tom*_*ote 5

Apple不相信退出按钮.但是你可以抛出另一个例外,你没有抓住导致你的应用程序崩溃,但如果你的应用程序崩溃,那么它将无法获得批准.

我认为最接近你可以通过在info.plist中将UIApplicationExitsOnSuspend设置为true来禁用后台处理,然后按主页按钮将退出你的应用程序.在这种情况下,您可以将退出按钮设置为指向任何其他应用程序的链接.

将if语句更改为始终引发异常应该会导致应用程序崩溃,以便它将退出.

- (void)handleException:(NSException *)exception
{
    [self validateAndSaveCriticalApplicationData];

    UIAlertView *alert =
        [[[UIAlertView alloc]
            initWithTitle:NSLocalizedString(@"Unhandled exception", nil)
            message:[NSString stringWithFormat:NSLocalizedString(
                @"You can try to continue but the application may be unstable.\n\n"
                @"Debug details follow:\n%@\n%@", nil),
                [exception reason],
                [[exception userInfo] objectForKey:UncaughtExceptionHandlerAddressesKey]]
            delegate:self
            cancelButtonTitle:NSLocalizedString(@"Quit", nil)
            otherButtonTitles:NSLocalizedString(@"Continue", nil), nil]
        autorelease];
    [alert show];

    CFRunLoopRef runLoop = CFRunLoopGetCurrent();
    CFArrayRef allModes = CFRunLoopCopyAllModes(runLoop);

    while (!dismissed)
    {
        for (NSString *mode in (NSArray *)allModes)
        {
            CFRunLoopRunInMode((CFStringRef)mode, 0.001, false);
        }
    }

    CFRelease(allModes);

    NSSetUncaughtExceptionHandler(NULL);
    signal(SIGABRT, SIG_DFL);
    signal(SIGILL, SIG_DFL);
    signal(SIGSEGV, SIG_DFL);
    signal(SIGFPE, SIG_DFL);
    signal(SIGBUS, SIG_DFL);
    signal(SIGPIPE, SIG_DFL);

    [exception raise];
}
Run Code Online (Sandbox Code Playgroud)