开发者

Where to initialize iPhone application defaults

开发者 https://www.devze.com 2023-02-07 07:11 出处:网络
I just started using the preferences pane to let users customize some settings in my iOS app, and it was pretty easy to create the Settings.bundle and access the information from it.My problem is that

I just started using the preferences pane to let users customize some settings in my iOS app, and it was pretty easy to create the Settings.bundle and access the information from it. My problem is that I read in the Apple docs that the settings should be programaticall开发者_JS百科y initialized:

It is recommended that you register any default preference values programmatically at launch time in addition to including them in your settings bundle property lists. For newly installed applications, default preference values from the application’s settings bundle are not set until the Settings application runs. This means that if the user runs your application before running Settings, the default values specified in your settings bundle will not be available. Setting such values programmatically at launch time ensures that your application always has appropriate values. To register default values programmatically, use the registerDefaults: method of the NSUserDefaults class.

Where in the app is this initialization done, and how can I be sure that I'm not overwriting a user-supplied value? Is this handled in some method of the App Delegate?


You should register your defaults before you try to access a value stored in your NSUserDefaults. You could do it in - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions.
Registering your defaults is fast, so there is no need to optimize this. Just do it at the launch of the app.

I store my userdefaults in a plist and register the content of this list, like this:

NSDictionary *dictionary = [NSDictionary dictionaryWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"DefaultUserDefaults" ofType:@"plist"]];
[[NSUserDefaults standardUserDefaults] registerDefaults:dictionary];

If you register your defaults like this you don't have to worry that you overwrite user supplied values. NSUserdefaults uses "domains" where it stores it's values. If you register your defaults they are stored in the registration domain. If a User stores a value those values are stored in the application domain.

If you try to get a value from NSUserdefaults it looks if the value is present in the application domain, and if it's not there it takes the value from the registration domain.


Edit:

Where to initialize iPhone application defaults

you would access those values (or better, the values that are stored in your nsuserdefaults, and those as a fallback if there are no user provided values) like this:

NSInteger minutesToWait = [[NSUserDefaults standardUserDefaults] integerForKey:@"MinutesToWait";
NSString *urlString = [[NSUserDefaults standardUserDefaults] stringForKey:@"DefaultURLStr"];

The plist is just another representation of a NSDictionary with keys and values. The key is the same key you use to access the userdefaults, and the value is your defaultvalue. Pretty straight forward.
It doesn't matter how you create the dictionary. You can do it in code as well.


As @fluchtpunkt suggested, you can register the defaults using:

NSDictionary *dictionary = [NSDictionary dictionaryWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"DefaultUserDefaults" ofType:@"plist"]];
[[NSUserDefaults standardUserDefaults] registerDefaults:dictionary];

Personally, I check for each value independently in my App Delegate.

#define kSettings [NSUserDefaults]
if([kSettings boolForKey:@"firstRun"] == nil){

  //   
  //   Set up the initial settings...
  //


  [kSettings setBool:NO forKey:@"firstRun"]; 
}

I write a "reset" method and then call it on first run. I prefer doing this in code, but you theoretically could use a plist. I just feel like it's one less place to go wrong.


You can use the same method inside your app delegate that you use to setup your initial window, didFinishLaunchingWithOptions:

However, you may also need some logic inside applicationWillEnterForeground:, because potentially your user could put your app into the background, change settings inside the settings app, then resume your app and expect those changes to have been applied.


@MatthiasBauch or @Moshe's answers will most likely work for most people, but for anyone who like me had their settings in a Root.plist file (I was using the InAppSettingsKit), you have to dig a bit deeper into that particular plist file to get the actual default values of the settings (since they're not at the top level of the plist, but nested under the key PreferenceSpecifiers). Here is a link to another answer here, containing the extra code that worked for me:

https://stackoverflow.com/a/10497898/381233

0

精彩评论

暂无评论...
验证码 换一张
取 消