NSUserDefaults do not save in iOS7? - ios

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"];

Related

NSUserDefaults Boolean returns false when using app extension

I am using Notification Extension in my application to change the notification sound.
When the user turns off the UISwitch in the settings page I save in the NSUserDefaults a boolean to keep tracking of the state.
However I have 2 different storyboards for two different languages, each has its own style.
When I am using the storyboard A and I print the state of the boolean in the console, it's printing the state correctly. But when I change the language hence a different storyboard B loads up. The boolean is always returning false.
Although the boolean always return false, I have tested the sound of the notification, it works correctly.
Any idea why the boolean is always returning false? I need the correct value in order to show the state correctly of the ringtone in the settings page.
Here's the code I am using:
- (void)setPlayDefaultSound:(BOOL)playDefaultSound
{
NSUserDefaults *def = [[NSUserDefaults alloc] initWithSuiteName:#"group.com.xx.xxx.NotificationServices"];
[def setBool:playDefaultSound forKey:#"playDefaultSound"];
[def synchronize];
}
- (BOOL)playDefaultSound
{
NSUserDefaults *def = [[NSUserDefaults alloc] initWithSuiteName:#"group.com.xx.xxx.NotificationServices"];
return [def boolForKey:#"playDefaultSound"];
}
And here's a screenshot of the Info.plist of the Notification Services Extension:
You are creating new instances of NSUserDefaults each time. Use the same instance each time.
Use this instead.
- (void)setPlayDefaultSound:(BOOL)playDefaultSound
{
[[NSUserDefaults standardUserDefaults] setBool:playDefaultSound forKey:#"playDefaultSound"];
[[NSUserDefaults standardUserDefaults] synchronize];
}
- (BOOL)playDefaultSound
{
return [[NSUserDefaults standardUserDefaults] boolForKey:#"playDefaultSound"];
}
There was a mismatch in the name in the capabilities, a typo.
It's working normally now.

Reading settings bundle first time not returning correct values

I have some code where I am reading a switch value from my settings bundle however on some instances it reads the wrong value.
This is what my code looks like, it's in my app delegate applicationDidBecomeActive
NSUserDefaults *settings = [NSUserDefaults standardUserDefaults];
NSNumber *branchString = [settings objectForKey:#"signout"];
BOOL userSignOut = [[settings objectForKey:#"signout"] boolValue];
NSLog(userSignOut ? #"Yes" : #"No");
What happens is when the app first loads I receive NO then when I switch to settings and turn my switch over to YES, when I log back in I still get NO however if I then switch between settings and my app again with out touch the switch in settings that's when I get YES even though it should have happened a step earlier.
Are you using this when setting the value?
[[NSUserDefaults standardUserDefaults] setObject:(id) forKey:(NSString *)];
[[NSUserDefaults standardUserDefaults] synchronize];
You can use NSUserDefaultsDidChangeNotification as observer.

iOS: NSDefault Will Not Save / Load When App Terminates

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.

NSUserDefaults synchronize not saving on

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.

IOS: store an array with NSUserDefault

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

Resources