use*_*293 270 iphone cocoa-touch objective-c ipad ios
我正在编写一个iPhone应用程序,我需要强制它退出,因为某些用户操作.清理内存后分配的应用程序,调用终止应用程序的适当方法是什么?
Aug*_*ust 271
在iPhone上没有退出应用程序的概念.应该导致应用退出的唯一操作是触摸手机上的"主页"按钮,这不是开发人员可以访问的内容.
根据Apple的说法,您的应用不应该自行终止.由于用户未点击"主页"按钮,因此返回主屏幕会让用户感觉您的应用已崩溃.这是令人困惑的非标准行为,应该避免.
小智 216
你试过exit(0)吗?
或者,[[NSThread mainThread] exit]虽然我没有尝试过,这似乎是更合适的解决方案.
Sal*_*lim 49
exit(0)对用户显示为崩溃,因此向用户显示确认消息.确认暂停(按主题按钮按下编程)并等待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);
}
}
Run Code Online (Sandbox Code Playgroud)
Wag*_*agh 40
点击此处查看问答:https://developer.apple.com/library/content/qa/qa1561/_index.html
问:如何以编程方式退出iOS应用程序?
没有为优雅地终止iOS应用程序提供API.
在iOS中,用户按下主页按钮以关闭应用程序.如果您的应用程序具有无法提供其预期功能的条件,建议的方法是为用户显示警告,指示问题的性质以及用户可能采取的操作 - 打开WiFi,启用位置服务等.允许用户自行决定终止申请.
警告:不要调用该
exit功能.应用程序调用exit将显示给用户崩溃,而不是执行正常终止并动画回到主屏幕.此外,可能无法保存数据,因为如果调用exit,则不会调用
-applicationWillTerminate:类似的UIApplicationDelegate方法.如果在开发或测试期间有必要终止您的应用程序,建议使用该
abort函数或assert宏
小智 39
它不是一种退出计划的方式,而是一种迫使人们戒烟的方法.
UIAlertView *anAlert = [[UIAlertView alloc] initWithTitle:@"Hit Home Button to Exit" message:@"Tell em why they're quiting" delegate:self cancelButtonTitle:nil otherButtonTitles:nil];
[anAlert show];
Run Code Online (Sandbox Code Playgroud)
rch*_*ier 13
经过一些测试,我可以说以下内容:
[UIApplication sharedApplication]会导致应用看起来像崩溃,但它会- (void)applicationWillTerminate:(UIApplication *)application在这之前调用;exit(0);也将终止应用程序,但它看起来"正常"(跳板的图标显示为预期,具有缩小效果),但它不会调用- (void)applicationWillTerminate:(UIApplication *)application委托方法.我的建议:
- (void)applicationWillTerminate:(UIApplication *)application代理人.exit(0);.小智 8
您的ApplicationDelegate会收到用户有意退出的通知:
- (void)applicationWillResignActive:(UIApplication *)application {
Run Code Online (Sandbox Code Playgroud)
当我收到此通知时,我只是打电话
exit(0);
Run Code Online (Sandbox Code Playgroud)
所有的工作都做到了.最好的是,这是戒烟的意图,这就是为什么这不应该是在那里调用它的问题.
在我的音频应用程序上,当人们在音乐还在播放时同步他们的设备时,有必要退出应用程序.一旦同步完成,我会收到通知.但在此之后立即退出应用程序实际上看起来像是崩溃.
所以我设置了一个标志,以便在下一次后台操作时真正退出应用程序.可以在同步后刷新应用程序.
小智 6
我的应用程序最近被拒绝了,我使用了一种无证的方法.从字面上看:
"不幸的是,它无法添加到App Store,因为它使用的是私有API.禁止使用非公开API,如iPhone开发人员计划许可协议第3.3.1节所述:
"3.3.1应用程序只能以Apple规定的方式使用Documented API,不得使用或调用任何私有API."
应用程序中包含的非公共API是terminateWithSuccess"
Apple说:
"警告:不要调用退出函数.调用exit的应用程序将显示给用户崩溃,而不是执行正常终止并动画回到主屏幕."
我认为这是一个不好的假设.如果用户点击退出按钮并显示一条消息,如下所示:"应用程序现在将退出.",它似乎没有崩溃.Apple应提供退出应用程序的有效方法(不是退出(0)).
您不应该直接调用该函数exit(0),因为它会立即退出应用程序并且看起来像您的应用程序崩溃了。因此最好向用户显示确认警报并让他们自己执行此操作。
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)
}
Run Code Online (Sandbox Code Playgroud)
self.askForQuit { (canQuit) in
if canQuit {
self.quit()
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
229032 次 |
| 最近记录: |