The values in the Settings.bundle file are for the settings application, which can populate the default values for your application. They are not used by your own application. But you can set the default settings yourself using the registerDefaults: NSUserDefaults method. This does not actually install them to disk, but simply gives "default values for default values": they are used when the user has not yet set the value.
RegisterDefaults setting: must be done before using default values. The "applicationDidFinishLaunching:" method that others have suggested for this is too late in most cases. By the time you call "applicationDidFinishLaunching:" your views are already loaded from nib files, and their "viewDidLoad:" methods have been called. And they can usually read custom defaults.
To ensure that the default settings are set before first use, I use the following utility class, which loads the values from the Root.plist file and sets them using "registerDefaults:". This class is used to read custom defaults instead of "[NSUserDefaults standardUserDefaults]". Instead, use "[Receive Settings]".
As a bonus, it also contains a method for registering default change notifications for the user, since I always forget how this is done.
#import "Settings.h" @implementation Settings static bool initialized = NO; + (void) setDefaults { NSString *bundlePath = [[NSBundle mainBundle] bundlePath]; NSString *settingsBundlePath = [bundlePath stringByAppendingPathComponent:@"Settings.bundle"]; NSBundle *settingsBundle = [NSBundle bundleWithPath:settingsBundlePath]; NSString *settingsPath = [settingsBundle pathForResource:@"Root" ofType:@"plist"]; NSDictionary *settingsDict = [NSDictionary dictionaryWithContentsOfFile:settingsPath]; NSArray *prefSpecifierArray = [settingsDict objectForKey:@"PreferenceSpecifiers"]; NSMutableDictionary *appDefaults = [[NSMutableDictionary alloc] init]; for (NSDictionary *prefItem in prefSpecifierArray) { NSString *key = [prefItem objectForKey:@"Key"]; if (key != nil) { id defaultValue = [prefItem objectForKey:@"DefaultValue"]; [appDefaults setObject:defaultValue forKey:key]; } } // set them in the standard user defaults [[NSUserDefaults standardUserDefaults] registerDefaults:appDefaults]; if (![[NSUserDefaults standardUserDefaults] synchronize]) { NSLog(@"Settings setDefaults: Unsuccessful in writing the default settings"); } } + (NSUserDefaults *)get { if (!initialized) { [Settings setDefaults]; initialized = YES; } return [NSUserDefaults standardUserDefaults]; } + (void) registerForChange:(id)observer selector:(SEL)method { [[NSNotificationCenter defaultCenter] addObserver:observer selector:method name:NSUserDefaultsDidChangeNotification object:nil]; } + (void) unregisterForChange:(id)observer { [[NSNotificationCenter defaultCenter] removeObserver:observer name:NSUserDefaultsDidChangeNotification object:nil]; }
source share