iosipadxamarin.iosxamarinroot.plist

Using Xamarin, why would my application setting default to false?


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!


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.

    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.