当我的应用程序在iOS上终止时,保存NSUserDefaults的正确方法是什么 [英] What is the correct way to save NSUserDefaults when my app is terminated on iOS
问题描述
当我的应用在iOS中完全退出时,我需要保存 NSUserDefault
。
不是 didenterbackground
和前景
。
仅当用户在iOS中使用减号( - )符号终止我的应用时。
所以我在 applicationWillTerminate $中编写以下代码c $ c> of
AppDelegate
。
- (void)applicationWillTerminate: (UIApplication *)application
{
[[NSUserDefaults standardUserDefaults] setBool:NO forKey:@iCloud];
[[NSUserDefaults standardUserDefaults] synchronize];
}
但它不保存到 NSUserDefault
。
那么我应该在哪里写保存我的数据?
来自 Apple Doc :
对于不支持后台执行或链接的应用程序对于iOS 3.x或更早版本的
,当用户
退出应用程序时,始终会调用此方法。对于支持后台执行的应用程序,当用户退出应用程序时,通常不会调用此方法
,因为在这种情况下app
只是移动到后台。但是,在应用程序在后台运行
(未暂停)的情况下,此方法可能会调用
,系统需要因某种原因终止它。
当app退出时,可能不会调用时,不需要调用 applicationWillTerminate
当您的应用支持后台执行时。
您可以使用 UIApplicationExitsOnSuspend 指定应该终止该应用,而不是在退出时将其移至后台。
我担心,没有任何委托方法可以狡猾地处理应用程序的力量 - 非常情况。
<希望这可以帮助您解决问题。
I need to save my NSUserDefault
when my app is completely quit in iOS.
not didenterbackground
and foreground
.
Only when user kill my app with minus(-) sign in iOS.
So i write following codes in applicationWillTerminate
of AppDelegate
.
- (void)applicationWillTerminate:(UIApplication *)application
{
[[NSUserDefaults standardUserDefaults] setBool:NO forKey:@"iCloud"];
[[NSUserDefaults standardUserDefaults] synchronize];
}
But it doesn't save to NSUserDefault
.
So where should i write to save my data?
From Apple Doc :
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.
It's not necessary that applicationWillTerminate
will be called when app exits, it may not be called when your app supports background execution.
You can use UIApplicationExitsOnSuspend to specifies that the app should be terminated rather than moved to the background when it is quit.
I afraid, there isn't any delegate method which will surly handle app's force-quite scenario.
Hope this may help you in solving your problem.
这篇关于当我的应用程序在iOS上终止时,保存NSUserDefaults的正确方法是什么的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!