I have a pretty simple root.plist file that looks like this:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>StringsTable</key>
<string>Root</string>
<key>PreferenceSpecifiers</key>
<array>
<dict>
<key>Type</key>
<string>PSGroupSpecifier</string>
<key>Title</key>
<string>User Info</string>
</dict>
<dict>
<key>开发者_开发百科Type</key>
<string>PSTextFieldSpecifier</string>
<key>Title</key>
<string>Username</string>
<key>Key</key>
<string>username_preference</string>
<key>DefaultValue</key>
<string></string>
<key>IsSecure</key>
<false/>
<key>KeyboardType</key>
<string>Alphabet</string>
<key>AutocapitalizationType</key>
<string>None</string>
<key>AutocorrectionType</key>
<string>No</string>
</dict>
<dict>
<key>Type</key>
<string>PSTextFieldSpecifier</string>
<key>Title</key>
<string>Password</string>
<key>Key</key>
<string>password_preference</string>
<key>DefaultValue</key>
<string></string>
<key>IsSecure</key>
<true/>
<key>KeyboardType</key>
<string>Alphabet</string>
<key>AutocapitalizationType</key>
<string>None</string>
<key>AutocorrectionType</key>
<string>No</string>
</dict>
<dict>
<key>Type</key>
<string>PSGroupSpecifier</string>
<key>Title</key>
<string>Application Info</string>
</dict>
<dict>
<key>Type</key>
<string>PSTitleValueSpecifier</string>
<key>Title</key>
<string>Version</string>
<key>Key</key>
<string>api_version_preference</string>
<key>DefaultValue</key>
<string>2.0</string>
<key>Value</key>
<string>2.0</string>
</dict>
</array>
</dict>
</plist>
All the fields is shown when navigating to the relevant settings page in the iphone's settings application, but i can't access api_version_preference from my application.
I've tried to print out the .plist file as a dictionary and I only see the values from the username/password fields, so i'm probably doing something wrong. Here is what I use when printing the data:
NSLog(@"defs: %@", [[NSUserDefaults standardUserDefaults] dictionaryRepresentation]);
Can anyone help? :)
You forgot to supply defaults to NSUserDefaults. NSUserDefaults doesn't store the setting if you haven't changed (ie saved) it.
create a NSDictionary of default values and set it with [[NSUserDefaults standardUserDefaults] registerDefaults:userDefaultsDefaults];
like this:
NSDictionary *userdefaultsDefaults = [NSDictionary dictionaryWithObjectsAndKeys:
@"2.0", @"api_version_preference",
nil];
[[NSUserDefaults standardUserDefaults] registerDefaults:userdefaultsDefaults];
do this with all the settings you use from NSUserDefaults
精彩评论