Reputation: 7576
I'm trying to add Matt Gallagher's global exception handler to one of my projects. Running his example project located at :
http://cocoawithlove.com/2010/05/handling-unhandled-exceptions-and.html
I run into an issue where I press Quit and the app doesn't quit. It just returns me to the app. I tried to kill the app with the kill() call, but am unable to get the app to quit.
The callback from the alertview only seems to handle the Continue case and does not handle forcing the app to quit.
- (void)alertView:(UIAlertView *)anAlertView clickedButtonAtIndex:(NSInteger)anIndex
{
if (anIndex == 0)
{
dismissed = YES;
}
}
I know apps, by their nature cannot quit themselves, but in this case if the app is crashing I'd like the user to push the Quit button and have the app quit.
Thanks!
Upvotes: 2
Views: 2644
Reputation: 590
Apple doesn't believe in quit buttons. But you could throw another exception that you don't catch causing your app to crash, but if your app crashes then it won't get approved.
I think the closest you can get it to just disable backgrounding by setting UIApplicationExitsOnSuspend to true in your info.plist and then pressing the home button will quit your app. You could make the quit button a link to any other app in that case.
Changing the if statement to always raise an exception should make your app crash so it will quit.
- (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];
}
Upvotes: 5