简体   繁体   English

应用退出时删除NSUserDefault键值

[英]Delete NSUserDefault key value when app quits

I have saved an NSString in my NSUserDefault with key @"userKey" . 我有救了NSString在我NSUserDefault与关键@"userKey"
I want to be able to reset this value to @"" when the app is totally shut down but not when it enters the background. 我希望能够在应用程序完全关闭时将此值重置为@"" ,但在进入后台时不能重置。

I have tried: 我努力了:

[[NSUserDefaults standardUserDefaults] setObject:@"" forKey:@"opponentFile"];

in my appdelegate's -applicationWillTerminate: but nothing happens. 在我的appdelegate的-applicationWillTerminate:但是什么也没发生。

When I open the app after completely closing it the default value is still there. 当我完全关闭应用程序后打开它时,默认值仍然存在。
How do I do this? 我该怎么做呢?

Permanent NSUserDefaults changes are scheduled at intervals. 定期安排NSUserDefaults永久更改。
You can instead force this by adding the following to your -applicationWillTerminate: . 您可以通过在-applicationWillTerminate:添加以下内容来强制执行此操作。

[[NSUserDefaults standardUserDefaults] synchronize];

Don't force this too frequently as it tends to reduce your app performance. 不要过于频繁地使用它,因为它会降低您的应用程序性能。


Or... you can just do 或者...你可以做

[[NSUserDefaults standardUserDefaults] setObject:@"" forKey:@"opponentFile"];

in the -didFinishLaunchingWithOptions: instead. -didFinishLaunchingWithOptions:
Seems like the same thing to me. 对我来说似乎是同一件事。

请尝试:

- (void)applicationDidReceiveMemoryWarning:(UIApplication *)application;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM