How to ship applications with some preferences set to a set value
I have one preference in my application that I want my app to be "shipped" wit开发者_Go百科h, how do I do this?
Thanks in advance.
Create an NSDictionary then use
[myDict setObject:@"defaultvalue" forKey:@"mykey"];
for each of your default values. Then use
[[NSUserDefaults standardUserDefaults] registerDefaults:myDict];
It is safe to always do this on startup as it won't overwrite settings. When you go to read the value for your settings via
[[NSUserDefaults standardUserDefaults] objectForKey:@"mykey"];
you will get your default value, or the value that you subsequently set.
Store this preference in your app bundle as part of some set of defaults. When your app launches for the first time (an no saved preferences exist) save this value to NSUserDefaults, the documents directory, or where ever you store your app's preferences.
If there is nothing set in the defaults for a given key, set something. You can do this check whenever your app loads, at any time after the first run, you'll always have an object for your @"MySpecialPref"
key.
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
if (![defaults objectForKey:@"MySpecialPref"]) {
[defaults setBool:YES forKey:@"MySpecialPref"];
}
You could also ship it with a default SQLite database that contains custom values, and use them in your app
精彩评论