我正在编程一个iPhone应用程序,我需要强制它退出由于某些用户操作。在清理应用程序分配的内存后,调用什么方法来终止应用程序?
当前回答
用home键以外的其他方式退出应用程序真的不是ios风格的方法。
我做了这个助手,虽然,没有使用私人的东西:
void crash()
{ [[NSMutableArray new] addObject:NSStringFromClass(nil)]; }
但对我来说还是不适合生产。它用于测试崩溃报告,或在核心数据重置后快速重新启动。只是使它安全不被拒绝,如果功能留在生产代码。
其他回答
我使用了上面提到的[[NSMutableArray new] addObject:nil]方法来强制退出(崩溃)应用程序,而不做一个泄露的退出(0)函数调用。
为什么?因为我的应用程序在所有网络API调用上都使用了证书钉住,以防止中间人攻击。这些包括我的财务应用程序在启动时所做的初始化调用。
如果证书认证失败,我的所有初始化调用错误,让我的应用程序在一个不确定的状态。让用户回家然后回到应用程序并没有帮助,除非应用程序已经被操作系统清除,否则它仍然是未初始化和不值得信任的。
因此,在这种情况下,我们认为最好弹出一个警告,通知用户应用程序正在不安全的环境中运行,然后,当他们点击“关闭”时,使用上述方法强制退出应用程序。
[[UIApplication sharedApplication] terminateWithSuccess];
它工作正常,自动调用
- (void)applicationWillTerminateUIApplication *)application delegate.
要删除编译时警告,请添加以下代码
@interface UIApplication(MyExtras)
- (void)terminateWithSuccess;
@end
你不应该直接调用函数exit(0),因为它会立即退出应用程序,看起来就像你的应用程序崩溃了。所以最好是给用户显示一个确认提醒,让他们自己去做。
斯威夫特4.2
func askForQuit(_ completion:@escaping (_ canQuit: Bool) -> Void) {
let alert = UIAlertController(title: "Confirmation!", message: "Do you want to quit the application", preferredStyle: .alert)
alert.addAction(UIAlertAction(title: "Yes", style: UIAlertAction.Style.default, handler: { (action) in
alert.dismiss(animated: true, completion: nil)
completion(true)
}))
alert.addAction(UIAlertAction(title: "No", style: UIAlertAction.Style.cancel, handler: { (action) in
alert.dismiss(animated: true, completion: nil)
completion(false)
}))
self.present(alert, animated: true, completion: nil)
}
/// Will quit the application with animation
func quit() {
UIApplication.shared.perform(#selector(NSXPCConnection.suspend))
/// Sleep for a while to let the app goes in background
sleep(2)
exit(0)
}
用法:
self.askForQuit { (canQuit) in
if canQuit {
self.quit()
}
}
嗯,如果你的应用需要网络连接,你可能“不得不”退出应用程序。你可以显示一个警告,然后做这样的事情:
if ([[UIApplication sharedApplication] respondsToSelector:@selector(terminate)]) {
[[UIApplication sharedApplication] performSelector:@selector(terminate)];
} else {
kill(getpid(), SIGINT);
}
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);
}
}
推荐文章
- 调整UITableView的大小以适应内容
- 在代码中为UIButton设置一个图像
- NSRange从Swift Range?
- UICollectionView中的单元格间距
- 我如何在我的iOS应用程序中每n分钟得到一个后台位置更新?
- 如何使用iOS创建GUID/UUID
- 禁用所呈现视图控制器的交互式撤销
- 点击按钮时如何打开手机设置?
- 如何使用UIVisualEffectView来模糊图像?
- 如何修复UITableView分隔符在iOS 7?
- 故事板中的自定义单元格行高设置没有响应
- 如何改变时间和时区在iPhone模拟器?
- 在Swift中使用自定义消息抛出错误/异常的最简单方法?
- 如何在Swift中获得唯一的设备ID ?
- 我如何知道何时UITableView完成了ReloadData?