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?
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 upNSRunLoop).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.