我正在编程一个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的答案一样
其他回答
您是否尝试过exit(0)?
或者,[[NSThread主线程]退出],虽然我没有尝试,这似乎是更合适的解决方案。
除了上面的,很好,答案我只想补充一下,想想清理你的记忆。
应用程序退出后,iPhone操作系统将自动清理应用程序遗留的所有内容,因此手动释放所有内存只会增加应用程序退出所需的时间。
我的应用程序最近被拒绝了,因为我使用了一个无文件记录的方法。字面意思:
“不幸的是,它不能添加到App Store,因为它使用的是私有API。如iPhone开发者程序许可协议第3.3.1节所述,禁止使用非公共api:
“3.3.1应用程序只能按照苹果规定的方式使用文档api,不得使用或调用任何私有api。”
包含在应用程序中的非公共API是terminateWithSuccess"
- (IBAction)logOutButton:(id)sender
{
//show confirmation message to user
CustomAlert* alert = [[CustomAlert alloc] initWithTitle:@"Confirmation" message:@"Do you want to exit?" delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"OK", nil];
alert.style = AlertStyleWhite;
[alert setFontName:@"Helvetica" fontColor:[UIColor blackColor] fontShadowColor:[UIColor clearColor]];
[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
NSLog(@"exit(0)");
exit(0);
}
}
点击这里查看问答: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
推荐文章
- 我如何获得iOS 7默认的蓝色编程?
- UITapGestureRecognizer破坏UITableView didSelectRowAtIndexPath
- 在Objective-C中@property保留,赋值,复制,非原子
- 6.5英寸屏幕的App store截图大小是多少?
- 我如何在NSAttributedString中创建一个可点击的链接?
- iOS测试/规格TDD/BDD以及集成和验收测试
- 停止UIWebView垂直“弹跳”?
- 启动屏幕故事板不显示图像
- 对未渲染的视图进行快照,结果是一个空快照
- 是否可以为iPhone应用程序(如YouTube和地图)注册一个基于http+域的URL方案?
- 模拟器错误fbssystemservicdomain代码4
- 开始使用instancetype而不是id是否有益?
- 改变UISegmentedControl的字体大小
- 我可以强制UITableView隐藏分隔符之间的空单元格吗?
- 为什么Objective-C文件使用。m扩展名?