Markus
Markus

Reputation: 1177

Get ios multi-value user preferences

I try to retrieve the value selected in Settings Application using "Settings Bundle". The field is "PSMultiValueSpecifier". In "Root.plist" I implemented:

And, In the implement file I have wrote:

    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    NSString *string = [defaults objectForKey:@"abcdefg"];

But "string" is empty. When I do the same with a user default of kind: "PSTextFieldSpecifier" I can retrieve the value.

I follow the instructions explained in the book "Apress - Beginning iOS 5 Development: Exploring the iOS SDK" but I can not retrieve the value selected.

Instructions are the same of the "Apple Development Help".

I do not understand what is the problem. It seems easy.

Upvotes: 2

Views: 6911

Answers (4)

Jim75
Jim75

Reputation: 767

if the returned object is nil then set the default value.

NSString * key = @"the key";
NSUserDefaults * uD = [NSUserDefaults standardUserDefaults];
NSString * stringValue = [uD objectForKey:key];

if (!stringValue) {
    stringValue = @"the default value";
    [uD registerDefaults:@{key: stringValue}];
}

Upvotes: 0

kaige
kaige

Reputation: 11

No, I think you need to register your default value, for example in:

 -(BOOL) application: (UIApplication*) application didFinishLaunchingWithOptions: (NSDictionary*) launchOptions
{

     NSDictionary* defaults = @{@"key": @"default value"};
     [[NSUserDefaults standardUserDefaults] registerDefaults:defaults];
}

This is because the app knows nothing about the settings bundle it contains. The page 430 of book Beginning iOS6 Development: Exploring the iOS SDK has a clear explanation of this.

Upvotes: 1

cod3monk3y
cod3monk3y

Reputation: 9843

It seems that DefaultValue is used only in the official Settings app. Apparently, you have to register the defaults by 1) testing one of your settings to see if it's NULL, then 2) iterating through the settings, grabbing and setting the defaults:

sample Obj-C code for this by just.do.it

The documentation on PSMultiValueSpecifier does seem deficient here. Confusion on this shows up all over SO and the web. For example:

Upvotes: 1

Markus
Markus

Reputation: 1177

I have found the bug.

If you implement "Multi Value" user preferences in "Settings.bundle" (with its "Default Value"), but never enter in settings iPhone simulator and never change the multi-value preference, the sentence:

    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    NSString *string = [defaults objectForKey:@"key"];

Returns a "nil" value. Incredible, I lost a week trying to find the problem!!!!

But I have a doubt:

If I implement a "Multi Value" user preferences and I publish the App in iTunes Store, but user not use this "Multi Value" user preferences, "NSUserDefaults" will return nill value?

Do I have to consider the possibility of receiving an empty value?

Really, I think it is ridiculous.

Upvotes: 2

Related Questions