1

I made custom plist with default settings according to this Apple documentation page and with help of this answer on question at SO.

Here is my app delegate code:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
   NSString *defaultPrefsFile = [[NSBundle mainBundle] pathForResource:@"defaultSettings" ofType:@"plist"];
   NSDictionary *defaultPreferences = [NSDictionary dictionaryWithContentsOfFile:defaultPrefsFile];
   [[NSUserDefaults standardUserDefaults] registerDefaults:defaultPreferences];

   // Override point for customization after application launch.
   return YES;
}

I also made plist with name defaultSettings .plist and store here my default settings.

here it's structure:

<dict>
    <key>Sound</key>
    <true/>
    <key>Music</key>
    <true/>
    <key>Difficulty</key>
    <integer>0</integer>
</dict>

In my settings VC, I set from plist my outlets and store to plist when values changes.

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.


    self.soundSwitcher.on = [[NSUserDefaults standardUserDefaults] boolForKey:@"Sound"];

    ....  

    self.difficultySelector.selectedSegmentIndex = [[NSUserDefaults standardUserDefaults] integerForKey:@"Difficulty"];
}

It seems alright but outlets doesn't set to my defaults that set in property list itself. I can't also change this value by IBAction method

- (IBAction)switchSound:(UISwitch *)sender
{
    [[NSUserDefaults standardUserDefaults] setBool:sender.isOn forKey:@"Sound"];
}

What I do wrong?

Edit: I want to mention, that if I close settings View Controller and then open it again, settings saves as I left it. (So, in fact all work). But If I relaunch my app, all settings is unset.

Community
  • 1
  • 1
Dima Deplov
  • 3,688
  • 7
  • 45
  • 77
  • Do you call `synchronize`? Is all your testing done connected to Xcode? Did you log when the defaults are set and when you try to read them? Are you using storyboard? Did you try setting the values in `viewWillAppear:`? – Wain Feb 27 '14 at 16:36
  • @Wain no, but I'll try this right now. Yes, my testing connected to Xcode. Yes, I log (values is unset when I open settings View Controller e.g. all bool = no, integer = 1, I have another sets in my plist). Yes, I use storyboard. No, I don't try to set outlets in `viewWillAppear:` but honestly, I don't think, I almost sure that this is not the case – Dima Deplov Feb 27 '14 at 16:46

1 Answers1

2

You forgot to write this after to change something in NSUserDefaults

[defaults synchronize];
Fran Martin
  • 2,369
  • 22
  • 19
  • okay, I'll try it right now, but according to documentation `Because this method is automatically invoked at periodic intervals, use this method only if you cannot wait for the automatic synchronization (for example, if your application is about to exit) or if you want to update the user defaults to what is on disk even though you have not made any changes.` So I think this for special cases – Dima Deplov Feb 27 '14 at 16:43
  • this made it, I think my trouble was with "fast testing" I stop'n'run project almost after setting value in View Controller interface (delay about 3-10 secs), and this is too fast for `Because this method is automatically invoked at periodic intervals, use this method only if you cannot wait for the automatic synchronisation`. Anyway that help. Thanks. – Dima Deplov Feb 27 '14 at 16:57
  • If you quit your app as a user would ("quit" on OSX, return to home screen on iOS) rather than via the stop button in Xcode, the system will take care of synchronizing before quitting. – Catfish_Man Mar 02 '14 at 02:15