Initial values for NSUserDefaults aka first run values

If you want initial values to NSUserDefaults, you can use registerDefaults method.

Consider that the first time you access the property with some key name the value will be either nil for objects, false for booleans or 0 for numbers.

So, prepare a plist file in your bundle that contains the default preferences and then use that plist to register the defaults.

Then in appDelegate.m add:

<code>NSString *defaultPrefsFile = [[NSBundle mainBundle] pathForResource:@"defaultPrefs" ofType:@"plist"];
NSDictionary *defaultPreferences = [NSDictionary dictionaryWithContentsOfFile:defaultPrefsFile];
[[NSUserDefaults standardUserDefaults] registerDefaults:defaultPreferences];</code>

Credits to Mr. Ole Begemann and John Topley

Leave a Reply

Your email address will not be published. Required fields are marked *