我正在编程一个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
其他回答
苹果说:
警告:不要调用exit函数。调用exit的应用程序将在用户看来已经崩溃,而不是执行优雅的终止并动画返回主屏幕。”
我认为这是一个错误的假设。如果用户点击退出按钮,出现类似“应用程序现在将退出”这样的消息,它似乎没有崩溃。苹果应该提供退出应用程序的有效方法(而不是exit(0))。
您是否尝试过exit(0)?
或者,[[NSThread主线程]退出],虽然我没有尝试,这似乎是更合适的解决方案。
我们不能使用exit(0), abort()函数退出应用程序,因为苹果强烈反对使用这些函数。不过,您可以将此函数用于开发或测试目的。
如果在开发或测试期间,有必要终止您的 建议使用应用程序、中止函数或断言宏
请找到这个苹果问答线程以获得更多信息。
由于使用此功能创建印象,应用程序正在崩溃。所以我得到了一些建议,比如我们可以显示警报与终止消息意识到用户关闭应用程序,由于某些功能不可用。
但是iOS人机界面启动和停止应用指南,建议永远不要使用退出或关闭按钮来终止应用。而不是他们建议展示适当的信息来解释情况。
iOS应用永远不会显示关闭或退出选项。人们不再使用 当他们切换到另一个应用程序,返回主屏幕,或放 他们的设备处于睡眠模式。 永远不要以编程方式退出iOS应用程序。人们倾向于这样解释 作为一个崩溃。如果有什么东西阻止你的应用程序运行 有意的,你需要告诉用户情况和解释什么 他们可以做些什么。
除了上面的,很好,答案我只想补充一下,想想清理你的记忆。
应用程序退出后,iPhone操作系统将自动清理应用程序遗留的所有内容,因此手动释放所有内存只会增加应用程序退出所需的时间。
在iPhone上,没有退出应用的概念。导致应用退出的唯一操作是触摸手机上的Home键,而这不是开发者可以访问的。
根据苹果公司的说法,你的应用程序不应该自行终止。因为用户没有点击Home键,所以任何返回到Home屏幕的操作都会给用户留下应用程序崩溃的印象。这是令人困惑的,不规范的行为,应该避免。
推荐文章
- 在Swift中如何调用GCD主线程上的参数方法?
- NSLayoutConstraints是可动画的吗?
- iOS -构建失败,CocoaPods无法找到头文件
- Xcode 4挂在“附加到(应用程序名称)”
- CFNetwork SSLHandshake iOS 9失败
- 请求失败:不可接受的内容类型:文本/html使用AFNetworking 2.0
- 缺少推荐的图标文件-该包不包含iPhone / iPod Touch的应用程序图标,像素为“120x120”,png格式
- 以编程方式创建segue
- 在Objective-C中@synchronized如何锁定/解锁?
- Xcode构建失败“架构x86_64未定义的符号”
- 如何使用Xcode创建。ipa文件?
- 动态改变UILabel的字体大小
- registerForRemoteNotificationTypes: iOS 8.0及以上版本不支持
- Xcode 4 -在新的Macintosh安装上的配置文件上“没有找到有效的签名标识”错误
- 新的自动引用计数机制是如何工作的?