我正在尝试将Matt Gallagher的全局异常处理程序添加到我的一个项目中。运行位于以下位置的示例项目:
http://cocoawithlove.com/2010/05/handling-unhandled-exceptions-and.html
我遇到一个问题,我按Quit键,但该应用程序无法退出。它只是使我回到应用程序。我试图通过kill()调用杀死该应用程序,但无法使该应用程序退出。
来自alertview的回调似乎只处理Continue情况,而不处理强制应用程序退出。
- (void)alertView:(UIAlertView *)anAlertView clickedButtonAtIndex:(NSInteger)anIndex
{
if (anIndex == 0)
{
dismissed = YES;
}
}
我知道应用程序本质上不能自行退出,但是在这种情况下,如果应用程序崩溃了,我希望用户按下“退出”按钮并使应用程序退出。
谢谢!
最佳答案
苹果不相信退出按钮。但是您可能会抛出另一个未被捕获的异常,导致您的应用程序崩溃,但是如果您的应用程序崩溃,它将不会获得批准。
我认为您可以通过在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];
}