gpt4 book ai didi

iOS - UncaughtExceptions 全局异常处理程序不允许应用程序退出

转载 作者:塔克拉玛干 更新时间:2023-11-02 09:34:42 26 4
gpt4 key购买 nike

我正在尝试将 Matt Gallagher 的全局异常处理程序添加到我的一个项目中。运行位于 :

的示例项目

http://cocoawithlove.com/2010/05/handling-unhandled-exceptions-and.html

我遇到了一个问题,即我按下“退出”但应用程序没有退出。它只是让我回到应用程序。我尝试使用 kill() 调用终止应用程序,但无法让应用程序退出。

来自 alertview 的回调似乎只处理 Continue 情况,不处理强制应用退出。

- (void)alertView:(UIAlertView *)anAlertView clickedButtonAtIndex:(NSInteger)anIndex
{
if (anIndex == 0)
{
dismissed = YES;
}
}

我知道应用程序本身不能自行退出,但在这种情况下,如果应用程序崩溃,我希望用户按下“退出”按钮并退出应用程序。

谢谢!

最佳答案

Apple 不相信退出按钮。但是您可以抛出另一个您未捕获的异常,导致您的应用程序崩溃,但如果您的应用程序崩溃,则它不会获得批准。

我认为最接近的方法是通过在您的 info.plist 中将 UIApplicationExitsOnSuspend 设置为 true 来禁用后台运行,然后按下主页按钮将退出您的应用程序。在这种情况下,您可以将退出按钮设为指向任何其他应用的链接。

将 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];
}

关于iOS - UncaughtExceptions 全局异常处理程序不允许应用程序退出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4350170/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com