NSUserDefaults Not Saving Data. Limits? - ios

I have such a problem: the syntax of saving data in userDefaults is clear and was working everyday in app. Today i have such a problem that nothing is stored after synchronize. P.S i have no syntax error.
here is an example of saving and reading:
[[NSUserDefaults standardUserDefaults] setBool:YES forKey:#"StackOverflow"];
[[NSUserDefaults standardUserDefaults] synchronize];
And reading
BOOL stackOverflow = [[NSUserDefaults standardUserDefaults] boolForKey:#"StackOverflow"];
I know information about mutableCopy but in this simple situation it's not needed.
So is there any limits for userDefaults? I am testing on real device and storage i have up to 50GB free. I know that userDefaults are stored in plist file which in critical situation could be 1MB but i even dont exceed 1kb :( ...
using latest xcode and latest iOS version on 6S.
Need Advice. Thanks For help.

So after half hour morning work i find the solution and actually the problem. Case such as mine in 90% it is developers fault, so it was.
I had more than 30 user defaults value's, in which for e.g 15 of them had initWithSuitName (Identifier), everything saved with identifier id was working well but saving value without it was broken because of preferance conflicts.
Solution: I replaced unnamed userDefaults with identifier and everything has fixed.
In Case of some problem with NSUserDefaults:
NSUserDefaults - is thread safe.
Synchronize - Saves data immediately
Saving Data - If app will terminate chance of losing data is 50%, after debug Stop button tapped is same scenario. P.S data lose will be 50% if it's not saved in right place.
Debuging NSUserDefaults - in debugger you can simply tipe po [[NSUserDefaults standardUserDefaults] dictionaryRepresentation] and po [[NSUserDefaults standardUserDefaults] valueForKey:#"KeyForSavedData"]
Shared Container Example - if you want to share property for example between app and extension you can simply type appGroup identifier inside initWithSuitName:#"com.Example.ExampleAPP"

I faced this issue before. I just remove the key before add it to new object.
Example
[NSUSER removeObjectForKey:#"savedData"];
[NSUSER setBool:YES forKey:#"savedData"];
[NSUSER synchronize];
Note:
If you are used so many no of NSUSER then it act like ridicules. This is by my little experience.

Related

How to check if current version of the app has launched before?

I'd like to show a user agreement / disclaimer the first time my iOS app is launched. Currently, I'm simply doing it with the following code in didFinishLaunchingWithOptions in the AppDelegate:
//Show disclaimer to user, if he never agreed to it yet
if(![[NSUserDefaults standardUserDefaults] boolForKey:kHasAgreedToDisclaimerKey])
[self showDisclaimer];
However, I need to show a new disclaimer every time a new version of the app is installed and launch for the first time.
One way I was thinking of solving this problem was by creating a different UserDefaults key for each app version, but that looks like it will leave a lot of junk keys on the user's device.
Is there a better way to solve this?
Close. Don't save something for each app version, just store the last opened app version and compare it to the current app version. Something like this:
//Get the last opened and current versions
NSString *lastOpenedV = [[NSUserDefaults standardUserDefaults] objectForKey:#"LastOpenedVersion"];
NSString *currentVersion = [[NSBundle mainBundle] objectForInfoDictionaryKey:#"CFBundleShortVersionString"];
//Show an alert if necessary
if (lastOpenedV == nil || ![lastOpenedV isEqualToString:currentVersion]) {
[self showDisclaimer];
//Update the last opened version
[[NSUserDefaults standardUserDefaults] setObject:currentVersion forKey:#"LastOpenedVersion"];
}
Sounds like you have the right idea.
I'd try something like this:
NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
NSString *storedVersion = [prefs objectForKey:#"AppVersion"];
NSDictionary *infoDict = [[NSBundle mainBundle] infoDictionary];
NSString *bundleVersion = [infoDict objectForKey:#"CFBundleVersion"];
if (storedVersion) {
if (![storedVersion isEqualToString:bundleVersion]) {
[prefs setObject:bundleVersion forKey:#"AppVersion"];
[prefs synchronize];
// Show disclaimer
}
} else {
[prefs setObject:bundleVersion forKey:#"AppVersion"];
[prefs synchronize];
}
Basically, you are going to copy the bundle version to userDefaults, and compare it on every launch. If it isn't present in defaults, then you know the app was just installed, and you want to display the disclaimer. If it is in the defaults, then you want to check it against the bundle version. Anytime the bundle version doesn't match, you know the app has been updated.
Edit: Looks like took too long to mock up my example. I'm ten minutes too late, and you've found an answer. But I'll leave it here anyways :).
NSUserdefaults will be deleted when the app gets deleted, though data will persist on update. If that is OK for you, you could save a version number of the accepted version there, as mentioned by #Nick in the comment.
To make your information really persistant, write a version key to the keychain when the user accepted.

NSUserDefaults not saving

I am having a problem in my sprite kit app where my NSUserDefaults variable is not working. In createSceneContents (which I know is being called)
if (![defaults objectForKey:#"obj"]) {
difficultyLabel.text = #"Difficulty: Easy";
[defaults setObject:#"Easy" forKey:#"obj"];
} else {
difficultyLabel.text = [NSString stringWithFormat:#"Difficulty: %#", [defaults objectForKey:#"diff"]];
}
and then when you click on the SKLabelNode to change the difficulty and this code is being called
if ([label.text isEqualToString:#"Difficulty: Easy"]) {
label.text = #"Difficulty: Hard";
[defaults setObject:#"Hard" forKey:#"obj"];
NSLog(#"%#",[defaults objectForKey:#"obj"]);
} else {
label.text = #"Difficulty: Easy";
[defaults setObject:#"Easy" forKey:#"obj"];
NSLog(#"%#",[defaults objectForKey:#"obj"]);
but when I stop the program and run it again, it always just says Difficulty: Easy. Any suggestions?
As rmaddy's answer explains, NSUserDefaults won't immediately write data to long-term storage.
The values are saved in temporary memory when you call setObject:forKey:, so if you ever try to call objectForKey: after setObject:forKey:, it will return the value in memory if it exists, and if not, it goes to look for what's been saved to long-term storage.
In the background, the device will eventually save these values to permanent storage. It's something the OS handles, but in normal operation of your app, it shouldn't be necessary to immediately store these values to permanent storage most of the time, so let the OS do this at times when its been optimized to do so (I imagine the OS probably synchs every app's user defaults at once on some regular schedule, and as much as possible, tries to do this when the processor is idle probably).
But with that said, as rmaddy explains, if you've got something that crucially needs to be saved to the permanent storage immediately, you can always call synchronize. Keep in mind though... as long as your app isn't killed while in debug mode, the values you've set to be stored in user defaults will be stored.
But for your own sake, if you want to be absolutely certain, you can put it a call to synchronize in applicationDidEnterBackground as rmaddy suggests. Keep in mind though, this method isn't called if you kill the app.
When you say you "stop the program", what do you mean? Are you running in the debugger and killing the app and rerunning it again? NSUserDefaults doesn't persist your changes immediately. Either call [defaults synchronize] after writing important data or at least add such a call to your app delegate's applicationDidEnterBackground method. And then be sure you put your app in the background before killing it to ensure the data is written first.
To store:
NSString * str = #"name123";
NSUserDefaults *def=[NSUserDefaults standardUserDefaults];
[def setObject:str forKey:#"Key"];
[def synchronize];
NSLog(#"str = = %#",str);
To Retrieve:
NSUserDefaults *def=[NSUserDefaults standardUserDefaults];
NSString *str2 = [def valueForKey:#"Key"];
NSLog(#" Saved str = = %#",str2);
I found another reason why NSUserDefaults not saving,following code can make value in NSUserDefaults not saving:
NSUserDefaults *def= [[NSUserDefaults standardUserDefaults] init];
...
NSUserDefaults *def= [[NSUserDefaults standardUserDefaults] initWithSuiteName:#"123"];
You can search NSUserDefaults in your project to catch those init twice bug.
I had this issue where it was working previously but then wasn't. I couldn't figure it out.
It turned out restarting my mac fixed it. There must be some issue with the prefsd daemon that runs in the background that can cause this I'm guessing

IPhone - One time setup screen [duplicate]

This question already has answers here:
Detect when an iOS app is launched for the first time? [closed]
(7 answers)
Closed 9 years ago.
I am developing an iphone app which ask for one time set up details like user name in the first two screens when the user launches the app for the first time. I dont want these 2 screens to come up when I am launching the app once the user has given the details.
I tried getting the info from internet but I was not able to how to find it out. May be I am missing the technical term or wordings involved in doing this. Could any one please help in accomplishing this scenario. Any sample code would be very helpful.
Thanks for your time
Just set a boolean variable to the user-defaults. The user-defaults is a way to save information to the phone, external from the application, that the app can call upon whenever you want.
Inside your app-delegate when it boots, check the user-defaults for a boolean.
To save a boolean to user-defaults:
NSValue *state = ... //Whatever state you want. NSValue allows for booleans.
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults setObject:obj forKey:#"notFirstTimeRun"];
[defaults synchronize];
To load the boolean from user-defaults:
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSValue *state = [defaults objectForKey:#"notFirstTimeRun"];
Do a check:
if ([state boolValue] == true){
//Has ran before, skip your UIViews or whatever
}
else{
//Has not ran before, do your setup or whatever.
}
What you are looking to do is basically store a flag that dictates whether the user has gone through the setup steps. And upon app launch, you check to see if that flag exists with the respective value to denote whether or not to show a setup screen. NSUserDefaults provides a really simple, easy way of persisting state across sessions.
How about setting a bool in AppDidFinishLaunching. You can check that bool everytime the app is launched and show or hide respective screen based on that.
This is how I would do it:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
if ([[NSUserDefaults standardUserDefaults] boolForKey:#"HasBeenLaunched"]){
// This is not the first time, therefore open app as usual
} else {
// This is the first time, show special views
[[NSUserDefaults standardUserDefaults] setBool:YES forKey:#"HasBeenLaunched"];
[[NSUserDefaults standardUserDefaults] synchronize];
}
}

is that possible to know my apps is first time run or not in xcode?

i create an apps that i want to prompt an message for the user.. maybe using UIAlertview. After that, if the user run the apps for the second time, the alert won't prompt up anymore.
is that possible? honestly i don't have any idea about how to doing this.
Any idea? I search on STO, actually this link, still confused.
what is NSUserDefaults? How can NSUserDefaults store this information? i mean this is my first time or second time.
thanks.
To know what's NSUserDefaults, I suggest to take a look the official doc.
And of course you can use it to fulfill your goal.
You use a user default to store information about the current amount of runs in the app.
More or less like:
BOOL isRunMoreThanOnce = [[NSUserDefaults standardUserDefaults] boolForKey:#"isRunMoreThanOnce"];
if(!isRunMoreThanOnce){
// Show the alert view
// Then set the first run flag
[[NSUserDefaults standardUserDefaults] setBool:YES forKey:#"isRunMoreThanOnce"];
[[NSUserDefaults standardUserDefaults] synchronize];
}
Yes, U can save a value in NSUserDefault for the first time in your app & set it some other value once you open the app.
like
if(![[NSUserDefault standardUserDefault] objectforKey:#"AppOpenFirstTime"])
{
// App Open First time
// Show Alert
[[NSUserDefault standardUserDefault] setObject:#"1" forKey:#"AppOpenFirstTime"]
}
You can check if you stored some value in NSUserDefaults
NSString *flag = [[NSUserDefaults standardUserDefaults] stringForKey:#"not_first_run"];
if (!flag) {
//first run, do somethig
}
and then set it to some value
[[NSUserDefaults standardUserDefaults] setObject:#"just any string" forKey:#"not_first_run"];
NSUserDefaults is a key-value store saved between your application launches.
You can do it exactly as cortez said in your link. NSUserDefaults is written to disc, and will be created and accessed from your app.
See this link
First Time when your application launch at that time boolForKey:#"AlreadyRan" is FALSE. after that set it TRUE.
if(![[NSUserDefaults standardUserDefaults] boolForKey:#"AlreadyRan"] )
{
[[NSUserDefaults standardUserDefaults] setBool:TRUE forKey:#"AlreadyRan"];
}
With the NSUserDefaults class, you can save settings and properties
related to application or user data.
The objects will be saved in what is known as the iOS “defaults system”.
The iOS defaults system is available throughout all of the code in your app, and any data saved to the defaults system will persist through application sessions.This means that even if the user closes your application or reboots their phone, the saved data will still be available the next time they open the app!

How to determine that user runs the app for the first time?

I got a problem of my iOS app recently. In my app, an instruction view will appear at the first time of running, then hide from then on. How can I implement this effect?
Try to use this function:
- (BOOL) isFirstRun
{
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
if ([defaults objectForKey:#"isFirstRun"])
{
return NO;
}
[defaults setObject:[NSDate date] forKey:#"isFirstRun"];
[[NSUserDefaults standardUserDefaults] synchronize];
return YES;
}
In your app delegate check for a key in the user defaults (your own custom key, something like "AppWasAlreadyStartedPreviously"). If the key doesn't exist yet, it's the first run. You can show your instruction view and add the key to the user defaults. The next time the user starts the app you'll find the key in the user defaults and know that it's not the first run.
See the documentation of NSUserDefaults.
Store a file and check if the file exsists every time when you start the app. If tr file does not exsists then show intro, and then create the file.
The only way I think is to store a value into a specified file, when run the app, you should check the value first, and then you can deal with the result whether the app has already been run.

Resources