2

我正在尝试将 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;
   }
}

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

谢谢!

4

1 回答 1

5

苹果不相信退出按钮。但是你可以抛出另一个你没有发现的异常导致你的应用程序崩溃,但是如果你的应用程序崩溃了,那么它就不会被批准。

我认为你可以通过在 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];
}
于 2010-12-03T22:44:56.097 回答