views:

375

answers:

4

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, http://stackoverflow.com/questions/1128539/).

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?

A: 

You should verify if code is reached or if you just have display issue.

NSLog's will clarify that.

If not reached, you need to prevent app shutdown, and you may need a delayed action to get out of that context for the alert call: [self performSelector: @selector(showAlert:) withObject:@"msg" afterDelay: 0.1];

If you are reaching it and execution context is not an issue but you simply are not seeing alert, then [alert show] may be not be top level in display. In such case, you may need to redirect message via showinview e.g. with actionsheet:

topDelegate=[[UIApplication sharedApplication] delegate]; topDelegateWindow=[topDelegate.window.subviews objectAtIndex:0];

[actionSheet showInView:topDelegateWindow];

JDPearlman
A: 

Do you really need to show the error message to the user? Perhaps it would be best if you could deal with the exception silently, and need not worry about resolving this issue anyway.

Nick Toumpelis
A: 

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.

benzado
"[[NSRunLoop currentRunLoop] run]" did the trick. Thanks!
Mark Smith
Thanks, I've posted another solution to this thread based on your answer.
David Lawson
A: 

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);
David Lawson
Looks good. Personally, I'd create a separate object to serve as the alert's delegate. Since we're in disaster mode it makes sense to be isolated from the rest of the app's code. But otherwise, looks good.
benzado