VansFannel
VansFannel

Reputation: 45911

How to work with Settings.bundle

I'm developing an iOS app with latest SDK.

I have created a Settings.bundle with a Root.plist and another four .plist and every setting on these files have each default value.

This is the first time I work with Settings.bundle and I'm lost. I have found this question where they said that I have to read Settings.bundle defaults values every time I run the application and I don't understand why.

I think I have to continue using NSUserDefaults here to read settings values.

I have these questions:

  1. How can I read the values set on these settings using Setting app?
  2. How can I know if the user changes one or more of these values? The user makes the app to goes background, open Setting app and change something. How can I know that programmatically?
  3. How can I save any modification (a change in a setting value) and see that change on Settings app?

By the way, now I have this code:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    // Override point for customization after application launch.

    // Set the application defaults
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    if (![defaults boolForKey:@"firstRun"])
    {
        NSDictionary *appDefaults = [PreferenceDefaultValues dictionary];
        [defaults registerDefaults:appDefaults];

        [[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"firstRun"];

        [defaults synchronize];
    }

    return YES;
}

Upvotes: 0

Views: 1284

Answers (1)

Scott Berrevoets
Scott Berrevoets

Reputation: 16946

  1. The identifier of any preference item in the Settings.bundle plist can be accessed with NSUserDefaults. For example, if you have a preference item with an identifier named allowDiagnostics, then you can access the value for that item with [NSUserDefaults standardUserDefaults] boolForKey:@"allowDiagnostics"] (assuming that it was a BOOL item).

  2. You can use the NSUserDefaultsDidChangeNotification to get notified of any changes to your preferences.

  3. The same way you read them. To continue the example of 1, [NSUserDefaults standardUserDefaults] setBool:YES forKey:@"allowDiagnostics"]. Note though, that allowing the user to change options from multiple places (Settings.bundle and somewhere in your app), can be confusing to the user.

Upvotes: 3

Related Questions