I have a couple questions regarding application preferences using NSUserDefaults.
In my last app I would link my preferences using bindings to NSUserDefaults, and this worked perfectly. In the application startup methods I could then perform actions based on the values stored in the NSUserDefaults.
The problem occured when I updated my application and re-distributed it, in which all the settings would get over-written with the preferences in my computer. So if someone changed the settings, and then received the update, their settings would get over-written with mine.
Looking back at the Apple documentation in regards to saving Preferences, I notice the following line which I didn’t use, which is used to write preferences to disk:
[defaults synchronize];
My question is, does this code write the preferences to the user’s harddrive, so that if I re-distribute the application with different settings, NSUserDefaults will read from the disk instead?
Sorry for the misunderstanding but I can’t find an answer.
Thanks in advance everyone.
If you want to use bindings to control your preferences, you should be using
NSUserDefaultsController. In either case, to set initial values for your defaults, you should create a dictionary with the initial values and then pass it toregisterDefaults:if you’re using vanilla NSUserDefaults, or one ofinitWithDefaults:initialValues:orsetInitialValues:if you’re using NSUserDefaultsController. Here’s an NSUserDefaultsExample: