Hi Doug, thanks for replying.
On the internet I indeed found the same thing you say:
Quote:
âthe values are nil the first time you run your application if the user hasn't run the settings app prior to the first run of the app itselfâ
|
I find that a little strange but if that is the way it works.. oke.
If an app has a large number of Application Preferences its not that efficient to check and set them one by one. I have found a pice of code which does all that for you: It loads the default values from all the AppPreferences within the Root.plist
Code:
Code:
- (void)registerDefaultsFromSettingsBundle {
// Find the Settings.bundle //
NSString *settingsBundle = [[NSBundle mainBundle] pathForResource:@"Settings" ofType:@"bundle"];
if(!settingsBundle) {
NSLog(@"Could not find Settings.bundle");
return;
}
// Load the Root.plist into a Dictionary //
NSDictionary *settings = [NSDictionary dictionaryWithContentsOfFile:[settingsBundle stringByAppendingPathComponent:@"Root.plist"]];
// Load the PreferenceSpecifiers entry into an Array //
NSArray *preferences = [settings objectForKey:@"PreferenceSpecifiers"];
// Create a new Dictionary which will contain all AppPreferences with their Defaut value //
NSMutableDictionary *defaultsToRegister = [[NSMutableDictionary alloc] initWithCapacity:[preferences count]];
// Walk through all AppPrefrences loaded from the Root.plist //
for(NSDictionary *prefSpecification in preferences) {
// Get the 'Key' item from then specific AppPreference //
NSString *key = [prefSpecification objectForKey:@"Key"];
if(key) {
// Add it to the defaultsToRegister Dictionary with its Default value //
[defaultsToRegister setObject:[prefSpecification objectForKey:@"DefaultValue"] forKey:key];
}
}
// Load the defaultsToRegister Dictionary into the UserDefaults class //
[[NSUserDefaults standardUserDefaults] registerDefaults:defaultsToRegister];
}
Just call it once, only if the defaults have not been loaded yet:
Code:
// Initialize the settings value first //
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
if(![defaults objectForKey:@"login_name"])
[self registerDefaultsFromSettingsBundle];
I hope this helps someone.