Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Showing an alert in an iPhone top-level exception handler

I'm trying to display a UIAlertView in a top-level iPhone exception handler. The handler function looks like this:

void applicationExceptionHandler(NSException *ex) {
  UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Error"
                                                      message:[ex reason]
                                                      delegate:nil
                                             cancelButtonTitle:@"OK"
                                             otherButtonTitles:nil];
  [alertView show];
}

I've seen similar code elsewhere (for instance, NSSetUncaughtExceptionHandler not catch all errors on iPhone).

If I single-step in the debugger, I can see that the exception handler is called, and I can see the current screen dim as if it's going to display the alert in front of it, but nothing appears. Outside of the debugger, the app just quits immediately and goes back to the system home screen.

It does work if I trap an error in applicationDidFinishLaunching and display an alert there before returning. I assume that the alert view never gets a chance to display in the exception handler because the app is terminating (as opposed to sitting there doing nothing if I just bail out of applicationDidFinishLaunching). Is there a way to make this work?

like image 334
Mark Smith Avatar asked Nov 24 '09 01:11

Mark Smith


2 Answers

Thanks heaps to benzado, here's what I think is a great generic top-level exception handler. I'm a beginner so hopefully it's done properly, but it does work :)

In my ...appDelegate.m:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{        
    [window makeKeyAndVisible];

    NSSetUncaughtExceptionHandler(&exceptionHandler);

    return YES;
}

BOOL exceptionAlertDismissed = FALSE;
void exceptionHandler(NSException *exception)
{
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"App Committed Suicide"
        message:@"Oh dear, that wasn't supposed to happen. You will have to restart the application... sorry!"
        delegate:[[UIApplication sharedApplication] delegate] cancelButtonTitle:nil otherButtonTitles:@"That's ok!", @"Erm, bye...", nil];
    [alert show];
    [alert release];

    while (exceptionAlertDismissed == FALSE)
    {
        [[NSRunLoop currentRunLoop] runUntilDate:[NSDate dateWithTimeIntervalSinceNow:0.1]];
    }
}

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    exceptionAlertDismissed = TRUE;
}

And in my ...appDelegate.h:

@interface ...appDelegate : NSObject <UIApplicationDelegate, UIAlertViewDelegate>
...
void exceptionHandler(NSException *exception);
like image 55
David Lawson Avatar answered Nov 03 '22 11:11

David Lawson


I don't know exactly how [alertView show] is implemented, but I imagine it makes some changes to the view hierarchy and then sets itself to display the alert on the next pass through the run loop (look up NSRunLoop).

But, since the app is about to quit, control doesn't return to the run loop, so the alert is never displayed. That's why you see the screen dim (the alert-level UIWindow is immediately added by show) but the alert doesn't appear (that would happen in the run loop).

If you include [[NSRunLoop currentRunLoop] run] at the end of your exception handler, the alert may appear.

If you want to let your app quit once the alert is done, you can probably do so by calling NSRunLoop's runUntilDate: in a while-loop, checking the value of a flag to see if the alert has been dismissed yet. If it has, simply exit the handler function and you're good to go. That means you'll have to set a delegate object on the alert which sets that flag.

If you want to let your app continue running... there I'm not so sure. You could just let the run loop continue to run out of the exception handler, but there might be bad/strange side effects to that. So you probably should let the app quit. Besides, if you're sure you can recover from the exception, you should have caught it somewhere.

like image 29
benzado Avatar answered Nov 03 '22 09:11

benzado