3
votes

I have an IOS5 project.

I have added a settings bundle to my project and added some settings to it.

I set properties of 'host_ip':

<dict>
        <key>AutocapitalizationType</key>
        <string>None</string>
        <key>AutocorrectionType</key>
        <string>No</string>
        <key>DefaultValue</key>
        <string>http://localhost</string>
        <key>IsSecure</key>
        <false/>
        <key>Key</key>
        <string>host_ip</string>
        <key>KeyboardType</key>
        <string>URL</string>
        <key>Title</key>
        <string>Host</string>
        <key>Type</key>
        <string>PSTextFieldSpecifier</string>
    </dict>

I try to read value like this:

NSUserDefaults *userDefaults =[NSUserDefaults standardUserDefaults];
NSString *host = [userDefaults stringForKey:@"host_ip"];
NSLog(@"%@",host);

It does not return the default value I set in host_ip, it returns nil.

How can I get my default value?

UPDATE

After a user edits a setting, I can retrieve it by the code above. My problem is getting the setting value, if it was not edited by the user yet. As I think in that case it should return the default I set in plist.

1
What do you mean "I have added a settings bundle to my project"? Do you mean that you've added a .plist file to your Xcode project? In order to read something from NSUserDefaults, it must be in your app's user defaults, which is generally stored in ~/Library/Preferences/<your app's bundle id>.plist.user1118321

1 Answers

2
votes

Did you register the defaults via NSUserDefaults?

See the registerDefaults: method on NSUserDefaults.