0

I am trying to save BOOL value and some simple string using NSUserDefaults. But NSUserdefaults is not saving any value. I have also used synchronize after saving the value into NSUserDefaults.The way I am using to save the BOOL into NSUserDefaults is as below.

[[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"loadfirsttimewindow"];
[[NSUserDefaults standardUserDefaults] synchronize];

And the way I am using to save the string into NSUserDefaults is as below.

[[NSUserDefaults standardUserDefaults] setValue:title1 forKey:@"item1"];
[[NSUserDefaults standardUserDefaults] synchronize];

So please let me know that where my NSUserDefaults value has been stored and why it is not saving any value into NSUserDefaults.

Simply I just want to save value which will remain saved even after the application has been closed.

The sample of code I have used for saving the value into NSUserDefaults in given below:

- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{

    NSLog(@"%hhd",[[NSUserDefaults standardUserDefaults] boolForKey:@"loadfirsttimewindow"]);
    if (![[NSUserDefaults standardUserDefaults] boolForKey:@"loadfirsttimewindow"])
    {
        [[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"loadfirsttimewindow"];
        [[NSUserDefaults standardUserDefaults] synchronize];
         *I have written code for the thing I have to do only when the application is launched for the first time.*
    }
    else
    {
       *I have written code for the thing I have to after the application is launched for the first time.*
    }
 }
swapnil patel
  • 384
  • 2
  • 17
  • 1
    How do you know the values are not being saved? BTW - do not use `setValue:forKey:`. Use `setObject:forKey:`. – rmaddy Feb 14 '18 at 05:58
  • @rmaddy I have saved the value into NSUserDefaults and then tried to print the value for that key. – swapnil patel Feb 14 '18 at 06:00
  • Update your question with code that fully demonstrates your issue. – rmaddy Feb 14 '18 at 06:01
  • @rmaddy I have added a sample of code. Like this, I have tried to save many values in NSUserDefaults. But any of values are not saved. – swapnil patel Feb 14 '18 at 06:10
  • i tried like this and result is, it only gives username and password to only one device which i design app. –  Feb 14 '18 at 06:11
  • Your code should work. How do you exit the application? By pressing stop in Xcode? Try quitting the app using the quit menu command of your app. – Dirk Feb 14 '18 at 17:49
  • Does your application have a unique/valid product bundle identifier (`CFBundleIdentifier`)? By default—no pun intended—`NSUserDefaults` stores your preferences in a file named after the product identifier, which you can verify by locating the file `~/Library/Preferences/.plist`. If your id isn't set/valid, they could be going into the bit bucket. – James Bucanek Feb 14 '18 at 17:54
  • @Dirk I have tried to install the app using Xcode as well also by exporting as a Mac app and then install. But in both case, NSUserDefaults is not saving the value. – swapnil patel Feb 15 '18 at 05:21

1 Answers1

1

I have found my solution from the link given below:

Mac sandbox created but no NSUserDefaults plist

When you move the container while testing / debugging to the trash, the cfprefsd (see Activity Monitor) still keeps a link to the .plist. Empty the trash and force quit both cfprefsd (user and root).

swapnil patel
  • 384
  • 2
  • 17