gpt4 book ai didi

ios - 什么时候调用 applicationWillTerminate?

转载 作者:IT王子 更新时间:2023-10-29 07:56:42 35 4
gpt4 key购买 nike

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

Apple 的文档对此含糊不清,它只说明系统何时因某种原因需要终止它。

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)。我已经得到了一些结果。

因此,当用户终止应用程序而不将其切换到后台模式时,将调用 applicationWillTerminate:应用程序处于事件状态,用户双击主页按钮并退出应用程序。

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

你可以检查这个:

- (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;

关于ios - 什么时候调用 applicationWillTerminate?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29416375/

35 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com