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


当前回答

Exit(0)在用户面前显示为崩溃,因此向用户显示确认消息。确认后暂停(home键按程序),等待2秒,应用程序将进入后台动画,然后退出用户视图

-(IBAction)doExit
{
    //show confirmation message to user
    UIAlertView* alert = [[UIAlertView alloc] initWithTitle:@"Confirmation"
                                                 message:@"Do you want to exit?"
                                                delegate:self
                                       cancelButtonTitle:@"Cancel"
                                       otherButtonTitles:@"OK", nil];
    [alert show];
}

-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if (buttonIndex != 0)  // 0 == the cancel button
    {
        //home button press programmatically
        UIApplication *app = [UIApplication sharedApplication];
        [app performSelector:@selector(suspend)];

        //wait 2 seconds while app is going background
        [NSThread sleepForTimeInterval:2.0];

        //exit app when app is in background
        exit(0);
    }
}

其他回答

[[UIApplication sharedApplication] terminateWithSuccess];

它工作正常,自动调用

- (void)applicationWillTerminateUIApplication *)application delegate.

要删除编译时警告,请添加以下代码

@interface UIApplication(MyExtras)
  - (void)terminateWithSuccess;
@end 

在application-info上添加UIApplicationExitsOnSuspend属性。请承认是真的。

我们不能使用exit(0), abort()函数退出应用程序,因为苹果强烈反对使用这些函数。不过,您可以将此函数用于开发或测试目的。

如果在开发或测试期间,有必要终止您的 建议使用应用程序、中止函数或断言宏

请找到这个苹果问答线程以获得更多信息。

由于使用此功能创建印象,应用程序正在崩溃。所以我得到了一些建议,比如我们可以显示警报与终止消息意识到用户关闭应用程序,由于某些功能不可用。

但是iOS人机界面启动和停止应用指南,建议永远不要使用退出或关闭按钮来终止应用。而不是他们建议展示适当的信息来解释情况。

iOS应用永远不会显示关闭或退出选项。人们不再使用 当他们切换到另一个应用程序,返回主屏幕,或放 他们的设备处于睡眠模式。 永远不要以编程方式退出iOS应用程序。人们倾向于这样解释 作为一个崩溃。如果有什么东西阻止你的应用程序运行 有意的,你需要告诉用户情况和解释什么 他们可以做些什么。

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

例如,假设用户退出了基于位置的应用程序,并使用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