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


当前回答

点击这里查看问答:https://developer.apple.com/library/content/qa/qa1561/_index.html

Q: How do I programmatically quit my iOS application? There is no API provided for gracefully terminating an iOS application. In iOS, the user presses the Home button to close applications. Should your application have conditions in which it cannot provide its intended function, the recommended approach is to display an alert for the user that indicates the nature of the problem and possible actions the user could take — turning on WiFi, enabling Location Services, etc. Allow the user to terminate the application at their own discretion. WARNING: Do not call the exit function. Applications calling exit will appear to the user to have crashed, rather than performing a graceful termination and animating back to the Home screen. Additionally, data may not be saved, because -applicationWillTerminate: and similar UIApplicationDelegate methods will not be invoked if you call exit. If during development or testing it is necessary to terminate your application, the abort function, or assert macro is recommended

其他回答

去你的信息。plist并检查关键“应用程序不运行在后台”。这一次,当用户单击home按钮时,应用程序将完全退出。

我的应用程序最近被拒绝了,因为我使用了一个无文件记录的方法。字面意思:

“不幸的是,它不能添加到App Store,因为它使用的是私有API。如iPhone开发者程序许可协议第3.3.1节所述,禁止使用非公共api:

“3.3.1应用程序只能按照苹果规定的方式使用文档api,不得使用或调用任何私有api。”

包含在应用程序中的非公共API是terminateWithSuccess"

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

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

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

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

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

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

我的建议:

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

用户应该决定应用程序何时退出。 我不认为当应用退出时这是一种良好的用户交互。因此,它没有很好的API,只有home按钮有一个。

如果有错误:更好地实现它或通知用户。 如果必须重新启动:执行它更好的通知用户。

这听起来很愚蠢,但在不让用户决定的情况下退出应用程序,并且不通知他,这是一种糟糕的做法。苹果表示,由于有一个home键用于用户交互,所以同一个功能(退出应用程序)不应该有两个东西。