NSUserDefaults Settings Bundle Plist
Asked Answered
B

2

1

I have an app that checks for a file upon load from the appDelegate's appDidFinishLoading method using a url value that I supposedly store in the NSUserDefaults Settings Root Plist:

NSString *pathStr = [[NSBundle mainBundle] bundlePath];
NSString *settingsBundlePath = [pathStr stringByAppendingPathComponent:@"Settings.bundle"];
NSString *finalPath = [settingsBundlePath stringByAppendingPathComponent:@"Root.plist"];
NSDictionary *settingsDict = [NSDictionary dictionaryWithContentsOfFile:finalPath];
NSArray *prefSpecifierArray = [settingsDict objectForKey:@"PreferenceSpecifiers"];

NSDictionary *prefItem;
for (prefItem in prefSpecifierArray){
    NSString *keyValueStr = [prefItem objectForKey:@"Key"];
    if ([keyValueStr isEqualToString:kFirstNameKey]){
        nsUserDefURL = [prefItem objectForKey:@"DefaultValue"];
    }
        if ([keyValueStr isEqualToString:kSecondNameKey]){
        NSLog(@"You are using local data:%@",[prefItem objectForKey:@"DefaultValue"]);
    }
}
NSLog(@" this is the url == %@", nsUserDefURL);

// since no default values have been set (i.e. no preferences file created), create it here     
NSDictionary *appDefaults = [NSDictionary dictionaryWithObjectsAndKeys:nsUserDefURL,kFirstNameKey,@"YES",kSecondNameKey,nil];
[[NSUserDefaults standardUserDefaults] registerDefaults:appDefaults];
[[NSUserDefaults standardUserDefaults] synchronize];

with some statics:

NSString *kFirstNameKey = @"url";
NSString *kSecondNameKey = @"Web DataSource";

When I NSLog the nsUserDefURL, i still get the default value in the settings plist. I went into the settings for the app (didn't know i could do so in the simulator) and I modified the value in the url field, but i STILL get the test2.xml value that is the default placeholder value in the settings root plist.

Here is the plist:Root.Plist

Or is it that the settings bundle doesn't work on the Xcode simulator?

Buonarroti answered 7/1, 2012 at 2:58 Comment(0)
B
0

Turns out I was missing this line:

nsUserDefURL = [[NSUserDefaults standardUserDefaults] stringForKey:kFirstNameKey];  

But this only reads the default value each launch. It won't save the user modified value in the app settings. Why is that value not being saved?

Buonarroti answered 10/1, 2012 at 22:6 Comment(0)
M
1

You'll need to explicitly save it. For example, if strFirstName is the value that the user modified, then you would save it to the app settings by

 NSUserDefaults* ud = [NSUserDefaults standardUserDefaults];
 [ud setObject:strFirstName forKey:kFirstNameKey];
 [ud sychronize];
Mccallum answered 24/5, 2012 at 6:52 Comment(0)
B
0

Turns out I was missing this line:

nsUserDefURL = [[NSUserDefaults standardUserDefaults] stringForKey:kFirstNameKey];  

But this only reads the default value each launch. It won't save the user modified value in the app settings. Why is that value not being saved?

Buonarroti answered 10/1, 2012 at 22:6 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.