winsmith
winsmith

Reputation: 21572

iPhone settings not honoured

My iPhone app has the following problem: Freshly installed, when I read out my "Play Sound" preference with the following code:

defaults = [NSUserDefaults standardUserDefaults];
NSLog(@"Play Sounds? %d", [defaults boolForKey:@"play_sounds_preference"]);

The setting always reads out as false, even though the default setting is set to true. Any ideas? Here is my Root.plist:

<?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>General Settings</string>
            </dict>
            <dict>
                <key>Type</key>
                    <string>PSToggleSwitchSpecifier</string>
                    <key>Title</key>
                    <string>Sounds</string>
                    <key>Key</key>
                    <string>play_sounds_preference</string>
                    <key>DefaultValue</key>
                    <true/>
            </dict>
    </array>
</dict>
</plist>

When the user opens the Settings.app and navigates to my app's name, THEN the setting reads out as true, even if the user doesn't change anything.

Upvotes: 1

Views: 335

Answers (1)

Tom Irving
Tom Irving

Reputation: 10059

The default setting is only set when the user opens the settings app, unfortunately.

You could simply check that the key exists when you start the app, and set the default if it doesn't:

if (![[NSUserDefaults standardUserDefaults] objectForKey:@"theKey"]){
    [[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"theKey"];
}

Or, if you have a lot of keys that need setting, you can look at NSUserDefault's registerDefaults.

Upvotes: 4

Related Questions