Question

I am running into a really strange problem. I have created an application setting for an iPad application, and for some reason the setting is defaulting to false- but only until the user opens the settings and observes the actual setting. After being observed at least once, the setting will always return the correct value, whether it is true or false (if the setting were a quantum particle it would make more sense to me). This behavior only occurs on device, not in simulator. Also, it is consistent when I launch in debug mode on device or when I release through TestFlight.

My setting is defined in Root.plist, which is located in my client solution in the Settings.bundle folder. All of the other settings defined in this file work fine.

Since I can't really show the Xamarin designer, here's my XML from Root.plist:

<dict>
    <key>Title</key>
    <string>Camera Settings</string>
    <key>Type</key>
    <string>PSGroupSpecifier</string>
</dict>
<dict>
    <key>DefaultValue</key>
    <true/>
    <key>Key</key>
    <string>useCameraForScanning</string>
    <key>Title</key>
    <string>Use Camera for Scanning</string>
    <key>Type</key>
    <string>PSToggleSwitchSpecifier</string>
</dict>

And here's my accessor code:

private const string useCameraForScanningKey = "useCameraForScanning";
private static bool useCameraForScanning = true;
public static bool UseCameraForScanning
{
    get
    { 
        bool useCamera = NSUserDefaults.StandardUserDefaults.BoolForKey(useCameraForScanningKey);
        useCameraForScanning = useCamera;
        return useCameraForScanning; 
    }
    set { NSUserDefaults.StandardUserDefaults.SetBool(value, useCameraForScanningKey); }    
}

Breakpoints throughout the accessor prove that the setting is being retrieved as false. I am very new to this Root.plist business, so I am guessing I am missing something obvious here.

Thanks in advance for any help offered!

Was it helpful?

Solution

Note that NSUserDefaults.BoolForKey returns NO or false if there is no settings for this key or if it is the first run of the application. It kinds of explains your problem, at least as far as I understood the problem.


Take a look on the Apple documentation:

boolForKey:

Returns the Boolean value associated with the specified key.

  • (BOOL)boolForKey:(NSString *)defaultName

Parameters

defaultName: A key in the current user's defaults database.

Return Value

If a boolean value is associated with defaultName in the user defaults, that value is returned. Otherwise, NO is returned.

OTHER TIPS

The link https://makeapppie.com/tag/root-plist/ clarifies a little more.

Many controls will have a  poorly named attribute Default Value. This is the value shown in  the settings app, not a true value of the default. It will show the values after the first use, but it is for display purposes not value purposes.

Here the solution

public static bool? NullableBoolForKey(this NSUserDefaults defaults, string key)
{
    var value = defaults.ValueForKey(new NSString(key));
    return value == null ? (bool?)null : ((NSNumber)value).UInt32Value == 1;
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top