我正在编程一个iPhone应用程序,我需要强制它退出由于某些用户操作。在清理应用程序分配的内存后,调用什么方法来终止应用程序?
当前回答
除了上面的,很好,答案我只想补充一下,想想清理你的记忆。
应用程序退出后,iPhone操作系统将自动清理应用程序遗留的所有内容,因此手动释放所有内存只会增加应用程序退出所需的时间。
其他回答
如果一个长期存在的应用程序也在后台执行,退出它可能是合适的,例如获取位置更新(使用位置更新后台功能)。
例如,假设用户退出了基于位置的应用程序,并使用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的答案一样
在iPhone上,没有退出应用的概念。导致应用退出的唯一操作是触摸手机上的Home键,而这不是开发者可以访问的。
根据苹果公司的说法,你的应用程序不应该自行终止。因为用户没有点击Home键,所以任何返回到Home屏幕的操作都会给用户留下应用程序崩溃的印象。这是令人困惑的,不规范的行为,应该避免。
点击这里查看问答: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
除了上面的,很好,答案我只想补充一下,想想清理你的记忆。
应用程序退出后,iPhone操作系统将自动清理应用程序遗留的所有内容,因此手动释放所有内存只会增加应用程序退出所需的时间。
苹果说:
警告:不要调用exit函数。调用exit的应用程序将在用户看来已经崩溃,而不是执行优雅的终止并动画返回主屏幕。”
我认为这是一个错误的假设。如果用户点击退出按钮,出现类似“应用程序现在将退出”这样的消息,它似乎没有崩溃。苹果应该提供退出应用程序的有效方法(而不是exit(0))。
推荐文章
- Xcode构建失败“架构x86_64未定义的符号”
- 如何使用Xcode创建。ipa文件?
- 动态改变UILabel的字体大小
- registerForRemoteNotificationTypes: iOS 8.0及以上版本不支持
- Xcode 4 -在新的Macintosh安装上的配置文件上“没有找到有效的签名标识”错误
- 新的自动引用计数机制是如何工作的?
- 如何测试对象在Objective-C中的类?
- 在iPhone上确定用户是否启用了推送通知
- 是否有可能禁用浮动头在UITableView与UITableViewStylePlain?
- 从Cocoa应用程序执行一个终端命令
- 错误ITMS-9000:“冗余二进制文件上传。火车1.0版本已经有一个二进制版本上传。
- Swift -转换为绝对值
- Swift编译器错误:“框架模块内的非模块化头”
- 从父iOS访问容器视图控制器
- 自定义dealloc和ARC (Objective-C)