2010-12-03 86 views
2

我正在尝试将Matt Gallagher的全局异常处理程序添加到我的其中一个项目中。运行位于他的榜样项目:iOS - UncaughtExceptions全局异常处理程序不允许应用程序退出

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

我碰上,我按下退出的问题和应用程序不退出。它只是让我回到应用程序。我尝试用kill()调用杀死应用程序,但无法让应用程序退出。

从alertview的回调似乎只处理继续的情况下,并没有处理强迫应用程序退出。

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

我知道的应用程序,根据其性质不能放弃自己,但在这种情况下,如果应用程序崩溃,我想用户按下退出键,并有应用程序退出。

谢谢!

回答

5

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

我认为最接近你可以通过在你的info.plist中将UIApplicationExitsOnSuspend设置为true来禁用背景,然后按下home按钮将退出你的应用程序。在这种情况下,您可以使退出按钮成为任何其他应用程序的链接。

将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]; 
} 
+1

如果您绝对需要强制关闭应用程序,您可以调用`abort()`。 – 2010-12-03 22:51:27