Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Having problems reading settings while using InAppSettingsKit

I am using the InAppSettingsKit modules to manage my settings.

I can include it in my project to the point where I can build a settings page that interacts correctly with my project. I have a root.plist that I can change and when I first run my app on the simulator I can see the changes reflected.

However, for the life of me, I cannot access the settings in NSUserDefaults through my code. When I run the code below I can loop through the contents of the [NSUserDefaults standardUserDefaults]. However, I only see a bunch of generic settings that are not related to the stuff in my plist file

NSUserDefaults* d = [NSUserDefaults standardUserDefaults];

NSDictionary* dict = [d dictionaryRepresentation];

NSString* descKey;
NSString* descObject;
NSString* classTypeForKey;
NSString* classTypeForObject;
id myObject;

for (id key in dict) {
    myObject = [dict objectForKey:key];
    classTypeForObject = [[myObject class] description];
    classTypeForKey = [[key class] description];
    descKey = [key description];
    descObject = [myObject description];
}

So where does InAppSettingsKit put the settings? The doc says in [NSUserDefaults standardUserDefaults] so I am at a loss.

like image 842
Steve Avatar asked Jul 12 '11 16:07

Steve


2 Answers

I think your problem relies on a misunderstanding of the concept. Root.plist doesn't store any settings but just defines how Settings.app and InAppSettingsKit work on your userDefaults.

When your app launches the first time, there are no userDefaults. Usually, you start by setting appropriate default settings by reading in a static userDefaults.plist from your Resources:

// Load the default values for the user defaults
NSString* pathToUserDefaultsValues = [[NSBundle mainBundle]
                                      pathForResource:@"userDefaults" 
                                      ofType:@"plist"];
NSDictionary* userDefaultsValues = [NSDictionary dictionaryWithContentsOfFile:pathToUserDefaultsValues];

// Set them in the standard user defaults
[[NSUserDefaults standardUserDefaults] registerDefaults:userDefaultsValues];
like image 106
Ortwin Gentz Avatar answered Sep 28 '22 06:09

Ortwin Gentz


Here is Ojas's code modified a bit further to account for ARC.

You would probably call initializeSettings from your AppDelegate didFinishLaunchingWithOptions.

#import "IASKSettingsReader.h"
...

- (void)initializeSettings
{
    // standard stored preference values
    NSUserDefaults* defaults = [NSUserDefaults standardUserDefaults];

    // settings files to process
    NSMutableArray *preferenceFiles = [[NSMutableArray alloc] init];

    // begin with Root file
    [preferenceFiles addObject:@"Root"];

    // as other settings files are discovered will be added to preferencesFiles
    while ([preferenceFiles count] > 0) {

        // init IASKSettingsReader for current settings file
        NSString *file = [preferenceFiles lastObject];
        [preferenceFiles removeLastObject];
        IASKSettingsReader *settingsReader = [[IASKSettingsReader alloc]
                                              initWithFile:file];

        // extract preference specifiers
        NSArray *preferenceSpecifiers = [[settingsReader settingsBundle]
                                         objectForKey:kIASKPreferenceSpecifiers];

        // process each specifier in the current settings file
        for (NSDictionary *specifier in preferenceSpecifiers) {

            // get type of current specifier
            NSString *type = [specifier objectForKey:kIASKType];

            // need to check child pane specifier for additional file
            if ([type isEqualToString:kIASKPSChildPaneSpecifier]) {
                [preferenceFiles addObject:[specifier objectForKey:kIASKFile]];
            }
            else {
                // check if current specifier has a default value
                id defaultValue = [specifier objectForKey:kIASKDefaultValue];

                if (defaultValue) {
                    // get key from specifier and current stored preference value
                    NSString *key = [specifier objectForKey:kIASKKey];
                    id value = [defaults objectForKey:key];

                    // update preference value with default value if necessary
                    if (key && value == nil) {
                        [defaults setObject:defaultValue forKey:key];
                    }
                }
            }

        }

    }

    // synchronize stored preference values
    [defaults synchronize];
}
like image 41
Peter Lamberg Avatar answered Sep 28 '22 05:09

Peter Lamberg