简体   繁体   中英

Delete NSUserDefault key value when app quits

I have saved an NSString in my NSUserDefault with key @"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.

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.
You can instead force this by adding the following to your -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.
Seems like the same thing to me.

请尝试:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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