何时调用applicationWillTerminate? [英] When will applicationWillTerminate be called?

查看:377
本文介绍了何时调用applicationWillTerminate?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在什么情况下会调用applicationWillTerminate?例如,如果代码崩溃,是否会偶尔调用?

In what situations will applicationWillTerminate be called? For example, will it ocassionally be called if there is a crash in the code?

Apple的文档对此含糊不清,它仅表示系统出于某种原因需要终止它的时间.

Apple's doc is vague on this, it only says when the system needs to terminate it for some reason.

对于不支持后台执行或已链接的应用程序 针对iOS 3.x或更早版本,此方法总是在用户 退出应用程序.对于支持后台执行的应用,此方法 用户退出应用程序时通常不会调用,因为该应用程序 在这种情况下,只需移至后台即可.但是,此方法可能 当应用在后台运行时被调用 (未暂停),并且系统出于某种原因需要终止它.

For apps that do not support background execution or are linked against iOS 3.x or earlier, this method is always called when the user quits the app. For apps that support background execution, this method is generally not called when the user quits the app because the app simply moves to the background in that case. However, this method may be called in situations where the app is running in the background (not suspended) and the system needs to terminate it for some reason.

推荐答案

我刚刚探讨了这个问题(iOS 9.2).而且我得到了一些结果.

I have just explored this question (iOS 9.2). And I have got some results.

因此,当用户终止应用程序而不将其切换到后台模式时,将调用applicationWillTerminate:该应用程序处于活动状态,用户双击主屏幕按钮并抛出该应用程序.

So, applicationWillTerminate is called when a user terminates the app without switching it to background mode: the app is active, the user makes double press on Home button and throws out the app.

但是,如果用户首先将应用切换到后台,然后在尝试终止该应用之后,将不会调用applicationWillTerminate.

But if a user switches the app to the background at first, and then after this tries to terminate the app, applicationWillTerminate will not be called.

您可以检查以下内容:

- (void)applicationWillTerminate:(UIApplication *)application {
    [[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"term"];
    [[NSUserDefaults standardUserDefaults] synchronize];
}

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {

    if ([[NSUserDefaults standardUserDefaults] boolForKey:@"term"]){

        [[NSUserDefaults standardUserDefaults] removeObjectForKey:@"term"];
        [[NSUserDefaults standardUserDefaults] synchronize];

        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"WORKED" message:@"term works" delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:nil];
        [alert show];

    }
...
return YES;

}

这篇关于何时调用applicationWillTerminate?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆