我正在编程一个iPhone应用程序,我需要强制它退出由于某些用户操作。在清理应用程序分配的内存后,调用什么方法来终止应用程序?


当前回答

我使用了上面提到的[[NSMutableArray new] addObject:nil]方法来强制退出(崩溃)应用程序,而不做一个泄露的退出(0)函数调用。

为什么?因为我的应用程序在所有网络API调用上都使用了证书钉住,以防止中间人攻击。这些包括我的财务应用程序在启动时所做的初始化调用。

如果证书认证失败,我的所有初始化调用错误,让我的应用程序在一个不确定的状态。让用户回家然后回到应用程序并没有帮助,除非应用程序已经被操作系统清除,否则它仍然是未初始化和不值得信任的。

因此,在这种情况下,我们认为最好弹出一个警告,通知用户应用程序正在不安全的环境中运行,然后,当他们点击“关闭”时,使用上述方法强制退出应用程序。

其他回答

这并不是一种退出项目的方法,而是一种强迫人们退出的方法。

UIAlertView *anAlert = [[UIAlertView alloc] initWithTitle:@"Hit Home Button to Exit" message:@"Tell em why they're quiting" delegate:self cancelButtonTitle:nil otherButtonTitles:nil];
[anAlert show];

经过一些测试,我可以这样说:

使用私有接口:[UIApplication sharedApplication]将导致应用程序看起来像它崩溃了,但它将调用- (void)applicationWillTerminate:(UIApplication *)应用程序之前这样做; 使用退出(0);也会终止应用程序,但它看起来“正常”(跳板的图标看起来像预期的一样,有缩小效果),但它不会调用- (void)applicationWillTerminate:(UIApplication *)应用程序委托方法。

我的建议:

在委托上手动调用- (void)applicationWillTerminate:(UIApplication *)应用程序。 调用退出(0);。

如果一个长期存在的应用程序也在后台执行,退出它可能是合适的,例如获取位置更新(使用位置更新后台功能)。

例如,假设用户退出了基于位置的应用程序,并使用home键将应用程序推到后台。在这种情况下,你的应用程序可能会继续运行,但完全退出它是有意义的。这将有利于用户(释放内存和其他不需要使用的资源),也有利于应用程序的稳定性(即确保应用程序在可能的情况下定期重新启动是防止内存泄漏和其他低内存问题的安全网)。

这可以(虽然可能不应该,见下面:-)实现如下:

- (void)applicationDidEnterBackground:(UIApplication *)application
{
    if (/* logged out */) {
        exit(0);
    } else {
       // normal handling.
    }
}

由于应用程序将退出后台,它不会看起来错误的用户,也不会像崩溃,提供用户界面是恢复下次他们运行的应用程序。换句话说,对用户来说,它不会看起来有任何不同的系统启动终止应用程序时,应用程序是在后台。

Still, it would be preferable to use a more standard approach to let the system know that the app can be terminated. For example in this case, by making sure the GPS is not in use by stopping requesting location updates, including turning off show current location on a map view if present. That way the system will take care of terminating the app a few minutes (i.e. [[UIApplication sharedApplication] backgroundTimeRemaining]) after the app enters the background. This would get all the same benefits without having to use code to terminate the app.

- (void)applicationDidEnterBackground:(UIApplication *)application
{
    if (/* logged out */) {
       // stop requesting location updates if not already done so
       // tidy up as app will soon be terminated (run a background task using beginBackgroundTaskWithExpirationHandler if needed).
    } else {
       // normal handling.
    }
}

当然,使用exit(0)对于在前台运行的普通生产应用程序是不合适的,就像其他参考http://developer.apple.com/iphone/library/qa/qa2008/qa1561.html的答案一样

在iPadOS 13中,你现在可以像这样关闭所有场景会话:

for session in UIApplication.shared.openSessions {
    UIApplication.shared.requestSceneSessionDestruction(session, options: nil, errorHandler: nil)
}

这将在你的应用委托上调用applicationWillTerminate(_ application: UIApplication),并在最后终止应用。

但要注意两件事:

这当然不是用来关闭所有的场景。(参见https://developer.apple.com/design/human-interface-guidelines/ios/system-capabilities/multiple-windows/) 它在iPhone的iOS 13上编译和运行良好,但似乎什么都不做。

更多关于iOS/iPadOS 13场景的信息:https://developer.apple.com/documentation/uikit/app_and_environment/scenes

这已经得到了一个很好的答案,但我决定扩展一下:

如果你不仔细阅读苹果的iOS人机界面指南,你的应用就不可能被AppStore接受。(他们保留拒绝你做任何反对他们的事情的权利)部分“不要以编程方式退出”http://developer.apple.com/library/ios/#DOCUMENTATION/UserExperience/Conceptual/MobileHIG/UEBestPractices/UEBestPractices.html 是一个明确的指导方针,告诉你应该如何处理这种情况。

如果你遇到苹果平台的问题,你很难找到解决方案,请咨询HIG。有可能苹果只是不想让你这么做,他们通常(我不是苹果,所以我不能保证总是)在他们的文档中这样说。