RegisterDefaults does not appear in the settings application

There are many answers related to using registering user defaults, and I looked hard, and I am still shocked by the behavior that I see.

In my set of settings, I have one toggle switch that I give the identifier 'sharing', giving it the default value of YES. I see this switch in the settings application, but it is not enabled by default. Even if I remove the application from the phone and reinstall it.

In my prefinishlaunchingwithoptions method, I have the following:

NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults]; NSDictionary *appDefaults = [NSDictionary dictionaryWithObject:@"YES" forKey:@"sharing"]; [defaults registerDefaults:appDefaults]; [defaults synchronize]; 

If I check that:

 NSLog(@"%d",[defaults boolForKey:@"sharing"]); 

The default value is set, but it is not reflected in the settings application.

If I then go to the settings app and turn on the switch and turn it on and check the value:

 NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults]; BOOL enabled = [defaults boolForKey:@"sharing"]; 

This will still result in the YES I expect.

And then I flip it back and test again:

 NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults]; BOOL enabled = [defaults boolForKey:@"sharing"]; 

It will be NO, as expected, and now the connection works just fine.

So, basically everything works fine, except that until the default setting is changed, it exists programmatically, but does not appear visually in the settings application.

Hope someone can point out what I am missing!

thanks

+4
source share
2 answers

The problem was that I had a value of “Value for ON” and “Value for Off.” To type String instead of boolean in my settings.bundle (Root.plist).

At first, I did not notice the error, because the application responded as expected, and can interact with the settings application. The only problem was that the initial “visual” value for the settings application was not set properly. This behavior makes sense when I think about it more.

This tutorial helped me find my mistake:

http://useyourloaf.com/blog/2010/5/18/adding-a-settings-bundle-to-an-iphone-app.html

+3
source

Using [NSUserDefaults standardUserDefaults] setBool: forKey:] instead of [[NSUserDefaults standardUserDefaults] setObject: forKey:] helped me simplify the casting.

+2
source

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


All Articles