I want to store an array with NSUserDefault, then, I put in applicationDidEnterBackground
[[NSUserDefaults standardUserDefaults] setObject:myArray forKey:#"myArray"];
and in application didFinishLaunchingWithOption
myArray= [[NSMutableArray alloc]
initWithArray:[[NSUserDefaults standardUserDefaults]
objectForKey:#"myArray"]];
it's ok for multitasking device, but for not-multitasking device, how can I solve?
Store the object in NSUserDefaults in -applicationWillTerminate:, if it hasn't already been saved by the invocation of -applicationDidEnterBackground: (i.e. check if multitasking is supported, if it is, then don't save it because it's already been saved.)
- (void) applicationWillTerminate:(UIApplication *) app {
if([[UIDevice currentDevice] respondsToSelector:#selector(isMultitaskingSupported)] &&
![[UIDevice currentDevice] isMultitaskingSupported]) {
[[NSUserDefaults standardUserDefaults] setObject:myArray forKey:#"myArray"];
}
}
Do not forget to sync the buffer before going into background:
[[NSUserDefaults standardUserDefaults] synchronize];
The previous answers are all correct, but note that neither applicationDidEnterBackground nor applicationWillTerminate are guaranteed to be called in all situations. You are usually better off storing important data whenever it has changed.
Save NSUserDefaults at
- (void)applicationWillTerminate:(UIApplication *)application
set
[[NSUserDefaults standardUserDefaults] setObject:myArray forKey:#"myArray"];
in
applicationWillTerminate
and don't forget to use the encodeWithCoder and initWithCoder inside the object that you are trying to save and that is contained in the array
Related
Here I can use NSUserDefaults, but I need to remove this NSUserDefaults.
In my app I'm using NSUserDefaults for getting locations one View to anotherView but here issue when I'm closed to the my app, and again I started still the locations are there, here I used for removing locations in AppDeligate
- (void)applicationDidEnterBackground:(UIApplication *)application
{
[[NSUserDefaults standardUserDefaults]removeObjectForKey:#"Location"];
}
But still locations is there.
How can I removed this?
Can you suggest me.
change your method
- (void)applicationDidEnterBackground:(UIApplication *)application
{
[[NSUserDefaults standardUserDefaults]removeObjectForKey:#"Location"];
[[NSUserDefaults standardUserDefaults] synchronize];
}
into
- (void)applicationWillTerminate:(UIApplication *)application
{
[[NSUserDefaults standardUserDefaults]removeObjectForKey:#"Location"];
[[NSUserDefaults standardUserDefaults] synchronize];
}
whenever you remove or add values in NSUserDefaults don't forget to call [[NSUserDefaults standardUserDefaults] synchronize];, if your fetching the values on that time no need to add [[NSUserDefaults standardUserDefaults] synchronize];
I want to open a ViewController only once when the app start for the very first time. Below is my code. The problem is when I am writing YES to NSUserdefaults , doing a synchronise , and then closing the app. in Xcode using simulator , the value is not updated to Yes.
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
if ([[NSUserDefaults standardUserDefaults] boolForKey:#"isFirstLaunch"])
{
// app already launched
[[NSUserDefaults standardUserDefaults] registerDefaults:[NSDictionary dictionaryWithObjectsAndKeys:[NSNumber numberWithBool:NO],#"isFirstLaunch", nil]];
[[NSUserDefaults standardUserDefaults]synchronize];
}
else
{
[[NSUserDefaults standardUserDefaults] registerDefaults:[NSDictionary dictionaryWithObjectsAndKeys:[NSNumber numberWithBool:YES],#"isFirstLaunch", nil]];
[[NSUserDefaults standardUserDefaults]synchronize];
}
return YES;
}
- (void)applicationDidBecomeActive:(UIApplication *)application
{
if ([[NSUserDefaults standardUserDefaults] boolForKey:#"isFirstLaunch"])
{
NSLog(#"first time");
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"Main" bundle:nil];
LASplashUserSettingViewController *splash = [storyboard instantiateViewControllerWithIdentifier:#"splash"];
[self.window.rootViewController presentViewController:splash animated:NO completion:nil];//
}
}
Why is NSUserDefaults not saving my values?
Have followed above and many other links. what am i missing here?
Thanks,
what am i missing here?
-registerDefaults: doesn't do what you think it does. Use -setBool:forKey: instead.
-registerDefaults: is used to ensure that values exist for certain keys in the defaults system. There's no need to call -synchronize after that method because the values in the registration domain are never saved. You probably meant to use that method to set up the default value YES for isFirstLaunch, but your code checks the value for the key before you register the default value! which defeats the purpose of the method. Proper use would be to call -registerDefaults: first, and then check the value associated with the key, and if the value is YES then call -setBool:forKey: to save the value NO.
A simpler option to detect the first launch would be to invert the question. Skip the -registerDefaults: call altogether and change the key to appHasRunPreviously. If no value is stored for a given key, -boolForKey: will return NO. If that happens, you know that this is the first time the app is running, and you should save YES for that key using -setBool:forKey:.
This is how you use NSUserDefaults in AppDelegate.m
+ (void)initialize
{
// Register defaults for first time
NSMutableDictionary *defs = [NSMutableDictionary dictionary];
[defs setObject:[NSNumber numberWithBool:YES] forKey:#"isFirstLaunch"];
[[NSUserDefaults standardUserDefaults] registerDefaults:defs];
}
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
if([defaults boolForKey:#"isFirstLaunch"])
{
// Do your stuff here
[defaults setBool:NO forKey:#"isFirstLaunch"];
[defaults synchronize];
}
}
Don't set isFirstLaunch back to YES in the else-condition if you only want the view to be shown once.
You can try this
[[NSUserDefaults standardUserDefaults]setBool:YES forKey:#"AlreadyLaunched"];
[[NSUserDefaults standardUserDefaults]boolForKey:#"AlreadyLaunched"]
In iOS6 the following code works fine. After the first launch, the hasLoaded BOOL is YES.
BOOL hasLoaded = [[NSUserDefaults standardUserDefaults] boolForKey:kUserDefaultFirstLaunch];
if (!hasLoaded){
[[NSUserDefaults standardUserDefaults] setBool:YES forKey:kUserDefaultFirstLaunch];
[[NSUserDefaults standardUserDefaults] synchronize];
//do first launch stuff
}
But in iOS7, every time I run the app, hasLoaded is NO, even though I'm clearly setting it to YES and synchronizing. I have seen a couple other posts about this but no solutions. This would be such a HUGE bug that I can't believe it's really an Apple bug. Please tell me I'm wrong. Is it really true that no apps can reliably use NSUserDefaults on iOS7?
I've tested this on both the simulator and the device.
After looking at the com.domain.myapp.plist file in the Preferences folder I saw that the kUserDefaultFirstLaunch was actually "2", which is neither true or false. Turns out that I had a typo elsewhere that was using the kUserDefaultFirstLaunch key.
Dumb mistake, but I'm glad it's not a bug in the SDK.
I had the same problem and I think adding the following to the Application Delegate was what fixed it:
- (void)applicationDidEnterBackground:(UIApplication *)application
{
[[NSUserDefaults standardUserDefaults] synchronize];
}
Take your value in string(or any other as per your requirement) and save the string in user defaults
NSUserDefaults *userdefault=[NSUserDefaults standardUserDefaults];
NSString *str=[NSString stringWithFormat:#"%#",[dicUser objectForKey:#"device_token"]];
[userdefault setObject:str forKey:#"YOURKEY"];
Dear Experts and Friends,
Hi, I have been trying to get this down for the whole day, but nothing I have tried seems to work. Basically I would like to save an array of strings (max: 20, nothing heavy weight) using NSDefault. It saves, it loads ok, but not when I killed the App on real device by pressing the Minus Button. I tested it on an iPhone 4gs (iOS 6).
I have tried [[NSUserDefaults standardUserDefaults] synchronize];
in both
applicationWillTerminate:(UIApplication *)application
and
applicationDidEnterBackground:(UIApplication *)application
But it didn't work.
Note: from NSLog, I am getting the values back fine from NSDefault as long as I don't kill the application in background on real device.
Does anyone know why? Many thanks in advance. Please cite with samples.
========================= EDIT (ADDING CODE):
I am adding in some code, in hopes that it will be clearer and that someone can really help me out here.
Thanks in advance.
some action, then
self.count = [NSNumber numberWithInt:[self.count intValue] + 1];
count is a NSNumber
NSData *count = [NSKeyedArchiver archivedDataWithRootObject:self.count];
[[NSUserDefaults standardUserDefaults] setObject:count forKey:#"count"];
[[NSUserDefaults standardUserDefaults] synchronize];
arrayForSavedKey is a NSMutableArray
[[NSUserDefaults standardUserDefaults] synchronize];
NSData *array = [NSKeyedArchiver archivedDataWithRootObject:self.arrayForSavedKey];
[[NSUserDefaults standardUserDefaults] setObject:array forKey:#"arrayForSavedKeys"];
In Both:
applicationWillResignActive:(UIApplication *)application
applicationWillEnterBackground:(UIApplication *)application
I have tried added the following line
[[NSUserDefaults standardUserDefaults] synchronize];
To retrieve the data, I have the Delegate to call a method in class A
by calling:
loadSavedData
and unarchiving like so:
NSData *savedKeys = [[NSUserDefaults standardUserDefaults] objectForKey:#"arrayForSavedKeys"];<br>
NSMutableArray *array = [[NSMutableArray alloc] initWithArray:[NSKeyedUnarchiver unarchiveObjectWithData:savedKeys]];<br>
self.arrayForSavedKey = [[NSMutableArray alloc] initWithArray: array];<br>
(There are only a few NSStrings in it)
In which I NSLOG the output and everything is fine SO LONG AS I do not kill the app by the Minus sign.
(But with a real device I cannot see anything from NSLog, of course, because once I killed the App, App will stop running as well in xCode)
I have spent all day today and I am running the end of my wits. Please help.
The applicationWillTerminate: is only called for a few very special cases so don't bother using that.
Using applicationWillEnterForeground: to save your defaults is way too late.
The proper place to call [[NSUserDefaults standardUserDefaults] synchronize]; is in the applicationDidEnterBackground: method. This way, if you app is killed in the background, the defaults have already been saved.
I have a bug submitted by a tester that if he performs an action and then reboots his phone (by pressing the home and Sleep/Wake button down for a few seconds) the app is not persisting state.
I have been able to reproduce this issue. [NSUserDefaults synchronize] is getting called but when I restart the app after the reboot, the value inside NSUserDefaults was not saved.
Does anybody know if synchronize stores to a buffer which is later saved to disk? If so, how do I flush the buffer (I thought synchronize was the same as a flush, but apparently not.)
(edit)
In other words:
assert([[NSUserDefaults standardUserDefaults] boolForKey: MY_KEY] == NO);
[[NSUserDefaults standardUserDefaults] setBool: YES forKey: MY_KEY];
[[NSUserDefaults standardUserDefaults] synchronize];
leave the app in the foreground and reboot the device after the above is called, then start the device back up and re-run the app. The assert should fire the second time around, but sometimes it doesn't.
To be very specific... I created a single view application and put the following code in the viewDidLoad
#define MY_KEY #"MY_KEY"
- (void)viewDidLoad
{
[super viewDidLoad];
BOOL key = [[NSUserDefaults standardUserDefaults] boolForKey: MY_KEY];
NSLog(#"[[NSUserDefaults standardUserDefaults] boolForKey: MY_KEY] == %#", key ? #"YES" : #"NO");
[[NSUserDefaults standardUserDefaults] setBool: !key forKey: MY_KEY];
[[NSUserDefaults standardUserDefaults] synchronize];
NSLog(#"reboot now.");
}
Here is the output of three runs of the app:
2013-05-31 12:17:44.127 syncTest[162:907] [[NSUserDefaults standardUserDefaults] boolForKey: MY_KEY] == YES
2013-05-31 12:17:44.133 syncTest[162:907] reboot now.
2013-05-31 12:18:49.771 syncTest[128:907] [[NSUserDefaults standardUserDefaults] boolForKey: MY_KEY] == NO
2013-05-31 12:18:49.778 syncTest[128:907] reboot now.
2013-05-31 12:19:41.388 syncTest[124:907] [[NSUserDefaults standardUserDefaults] boolForKey: MY_KEY] == NO
2013-05-31 12:19:41.397 syncTest[124:907] reboot now.
Note that the output was "YES, NO, NO" but it should have been "YES, NO, YES"
i had gone through the same problem and i identify that i had two reference of [NSUserDefaults standardUserDefaults] object on different classes because nsdefault is a singleton class soo the one called it earlier will have it if u might had already reference of [NSUserDefaults standardUserDefaults] try to finish it from the first reference and let the instance to be free from there and then call the instance at your other class and synchronize hopefully will be done
In my case, some values I wrote to the NSUserDefaults did persist and some did not. After lots of searching, I noticed the the key-value pair that did not save had a key that was capipatilized, i.e. TheKey v.s theKey. When I changed my key to a camel-cased key (theKey instead of TheKey), the value did persist thru app quit and re-launch. It seems very weird to me why that would be, but it worked in my case.