Where to initialize iPhone app settings

I just started using the settings panel so that users can configure some settings in my iOS application, and it was pretty easy to create a Settings.bundle file and access information from it. My problem is that I read in Apple docs that the settings must be programmatically initialized:

It is recommended that you register any default preference values ​​programmatically at startup, and include them in the property lists of the parameter list. For newly installed applications, default preferences from the set of application parameters are not set until the Settings application is launched. This means that if a user launches your application before launching the settings, the default values ​​specified in your parameter set will not be available. Setting these values ​​programmatically during startup ensures that your application always has the appropriate values. To programmatically register default values, use the registerDefaults: NSUserDefaults class method.

Where in the application is this initialization performed and how can I be sure that I am not overwriting the value provided by the user? Is this done in any application delegate method?

+4
source share
4 answers

You must register your default values ​​before attempting to access the value stored in NSUserDefaults. You can do this in - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions .
Registration of default settings is quick, so there is no need to optimize this. Just do it when you start the application.

I save my user IDs in plist and register the contents of this list, for example:

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

If you register your default values ​​like this, you don’t have to worry about overwriting user-supplied values. NSUserdefaults uses "domains" where it stores its values. If you register your default values, they are saved in the registration domain. If the user saves the value, these values ​​are stored in the application domain.

If you try to get a value from NSUserdefaults, then see if this value is present in the application domain, and if not, it takes a value from the registration domain.


Edit:

plist editor

you will get access to these values ​​(or, better, the values ​​that are stored in your nsuserdefaults, and those that are backup if there are no values ​​provided by the user):

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

Plist is just another NSDictionary representation with keys and values. The key is the same key that you use to access user identifiers, and the value is your default value. Pretty straight forward.
It doesn't matter how you create the dictionary. You can do this in code too.

+11
source

As suggested by @fluchtpunkt, you can register default values ​​using:

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

Personally, I check every value regardless of my application 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 the first time I run it. I prefer to do this in code, but theoretically you can use plist. I just feel that this is another place to go wrong.

+1
source

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

However, you may also need some logic inside applicationWillEnterForeground: because potentially your user can put your application in the background, change the settings inside the settings application, and then resume your application and expect these changes to be applied.

0
source

The @MatthiasBauch or @Moshe answers will most likely work for most people, but for those who, like me, had their settings in the Root.plist file (I used InAppSettingsKit ), you need to go a little deeper into this specific plist file so that get the actual default values ​​for the settings (since they are not at the top level of the plist, but nested under the key PreferenceSpecifiers ). Here is a link to another answer containing additional code that worked for me:

fooobar.com/questions/390018 / ...

0
source

Source: https://habr.com/ru/post/1337666/


All Articles