ToggleSwitch in preset not working on iPhone?

I have one Toggle Switch in my parameter set, but it does not work the first time. When I changed the value in the settings again, it works correctly.

Even I synchronize before using the value:

NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults]; [userDefaults synchronize]; if([userDefaults boolForKey:@"KeyName"]) { //Do Some Work } 

What do I need to do now?

+4
source share
2 answers

I think that here you are faced with a rather strange problem that the settings in your parameter set do not load until the user launches the settings application.

Straight from Apple:

For newly installed applications, the default preference value from the application. The settings package is not installed until the Settings application runs. This means that if a user launches your application before launching the Settings, the specified default values ​​in your "Settings" set are not available.

See: Documentation for Apple for more information.

+4
source

I assume you encountered this problem since you did not register your default values.

 [[NSUserDefaults standardUserDefaults] registerDefaults:[NSDictionary dictionaryWithObjectsAndKeys:@"defaultValueFOrKey", @"Key", nil]]; 

In normal practice, we will create a plist file with default values ​​for all keys in the parameter set for this purpose and register it as

 [[NSUserDefaults standardUserDefaults] registerDefaults:[NSDictionary dictionaryWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"Defaults" ofType:@"plist"]]]; 
+5
source

All Articles