我正在编程一个iPhone应用程序,我需要强制它退出由于某些用户操作。在清理应用程序分配的内存后,调用什么方法来终止应用程序?
当前回答
用home键以外的其他方式退出应用程序真的不是ios风格的方法。
我做了这个助手,虽然,没有使用私人的东西:
void crash()
{ [[NSMutableArray new] addObject:NSStringFromClass(nil)]; }
但对我来说还是不适合生产。它用于测试崩溃报告,或在核心数据重置后快速重新启动。只是使它安全不被拒绝,如果功能留在生产代码。
其他回答
如果一个长期存在的应用程序也在后台执行,退出它可能是合适的,例如获取位置更新(使用位置更新后台功能)。
例如,假设用户退出了基于位置的应用程序,并使用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)在用户面前显示为崩溃,因此向用户显示确认消息。确认后暂停(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
在iPhone上,没有退出应用的概念。导致应用退出的唯一操作是触摸手机上的Home键,而这不是开发者可以访问的。
根据苹果公司的说法,你的应用程序不应该自行终止。因为用户没有点击Home键,所以任何返回到Home屏幕的操作都会给用户留下应用程序崩溃的印象。这是令人困惑的,不规范的行为,应该避免。
我使用了上面提到的[[NSMutableArray new] addObject:nil]方法来强制退出(崩溃)应用程序,而不做一个泄露的退出(0)函数调用。
为什么?因为我的应用程序在所有网络API调用上都使用了证书钉住,以防止中间人攻击。这些包括我的财务应用程序在启动时所做的初始化调用。
如果证书认证失败,我的所有初始化调用错误,让我的应用程序在一个不确定的状态。让用户回家然后回到应用程序并没有帮助,除非应用程序已经被操作系统清除,否则它仍然是未初始化和不值得信任的。
因此,在这种情况下,我们认为最好弹出一个警告,通知用户应用程序正在不安全的环境中运行,然后,当他们点击“关闭”时,使用上述方法强制退出应用程序。
推荐文章
- 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)