我正在编程一个iPhone应用程序,我需要强制它退出由于某些用户操作。在清理应用程序分配的内存后,调用什么方法来终止应用程序?
当前回答
你不应该直接调用函数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()
}
}
其他回答
Swift 4.2(或更老版本)
库称为达尔文可以使用。
import Darwin
exit(0) // Here you go
注意:在iOS应用程序中不建议这样做。
这样做会得到崩溃日志。
- (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);
}
}
这已经得到了一个很好的答案,但我决定扩展一下:
如果你不仔细阅读苹果的iOS人机界面指南,你的应用就不可能被AppStore接受。(他们保留拒绝你做任何反对他们的事情的权利)部分“不要以编程方式退出”http://developer.apple.com/library/ios/#DOCUMENTATION/UserExperience/Conceptual/MobileHIG/UEBestPractices/UEBestPractices.html 是一个明确的指导方针,告诉你应该如何处理这种情况。
如果你遇到苹果平台的问题,你很难找到解决方案,请咨询HIG。有可能苹果只是不想让你这么做,他们通常(我不是苹果,所以我不能保证总是)在他们的文档中这样说。
在application-info上添加UIApplicationExitsOnSuspend属性。请承认是真的。
在iPadOS 13中,你现在可以像这样关闭所有场景会话:
for session in UIApplication.shared.openSessions {
UIApplication.shared.requestSceneSessionDestruction(session, options: nil, errorHandler: nil)
}
这将在你的应用委托上调用applicationWillTerminate(_ application: UIApplication),并在最后终止应用。
但要注意两件事:
这当然不是用来关闭所有的场景。(参见https://developer.apple.com/design/human-interface-guidelines/ios/system-capabilities/multiple-windows/) 它在iPhone的iOS 13上编译和运行良好,但似乎什么都不做。
更多关于iOS/iPadOS 13场景的信息:https://developer.apple.com/documentation/uikit/app_and_environment/scenes
推荐文章
- 保留HTML字体大小时,iPhone的方向从纵向改变为横向
- iPhone上UIView和UILabels的渐变
- keychain上的分发证书中缺少私钥
- 在实现API时,我如何避免在块中捕获自我?
- 如何创建一个Swift Date对象?
- Xcode 4在目标设备上说“finished running <my app>”——什么都没有发生
- 从另一个应用程序打开设置应用程序
- 快速提取正则表达式匹配
- 如何应用梯度的背景视图的iOS Swift应用程序
- 图书馆吗?静态的?动态吗?或框架?另一个项目中的项目
- 如何用SwiftUI调整图像大小?
- Xcode 6 gitignore文件应该包括什么?
- 如何在iPhone/iOS上删除电话号码的蓝色样式?
- 检测视网膜显示
- 如何在UIImageView中动画图像的变化?