Access to Xamarin.iOS.Bundle settings?

I tried for quite some time to find a solution to the problem, which should be fairly simple. I want variables that can be edited in the iPhone settings menu while the application is not running. Basically a configuration file wrapped in the iOS GUI.

This is supposed to be a built-in function in iOS, and while I can find some methods related to it, I cannot find the actual solution.

In the closest way, I get what I want, where it works, like any other variable: it is empty when the application starts and will be scratched again when the application is closed. And still does not appear in the iPhone settings window.

This is the code I have:

private void LoadSettingsFromIOS()
{
    // This is where it works like any other variable. Aka. gets scratched on app closing.
    _thisUser.SetValueForKey(new NSString("Blargh"), new NSString("SaveCredentials"));
    string stringForKey = _thisUser.StringForKey("SaveCredentials");

    // This is where I'm supposed to be able to load the data from settings and set the checkbox 'On' state to the value. Currently it always returns False.
    bool saveCredentials = _thisUser.BoolForKey("SaveCredentials");
    chckBoxRememberMe.On = saveCredentials;
}

And my Settings.Bundle Root.pList file:

  <?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>PreferenceSpecifiers</key>
      <array>
          <dict>
              <key>Type</key>
              <string>PSToggleSwitchSpecifier</string>
              <key>Title</key>
              <string>Credentials</string>
              <key>Key</key>
              <string>SaveCredentials</string>
              <key>DefaultValue</key>
              <true/>
          </dict>
      </array>
      <key>StringsTable</key>
      <string>Root</string>
  </dict>
  </plist>

Anyone who was messing around with Xamarin iOS and know how it works?

+4
2

EDIT: Xamarin: https://github.com/xamarin/monotouch-samples/tree/master/AppPrefs

iPhone.

"Settings.bundle" . "Root.plist". Property List. , Settings.bundle, Add → New File... → iOS ( ) → . .plist, iPhone.

Root.plist , .

, , : https://developer.apple.com/library/ios/DOCUMENTATION/Cocoa/Conceptual/UserDefaults/Preferences/Preferences.html

4-2 ( ). , Root.plist Xamarin, .

, ( , ):

public class Settings
{
    public static string ApiPath { get; private set; }

    const string API_PATH_KEY = "serverAddress"; //this needs to be the Identifier of the field in the Root.plist

    public static void SetUpByPreferences()
    {
        var testVal = NSUserDefaults.StandardUserDefaults.StringForKey(API_PATH_KEY);

        if (testVal == null)
            LoadDefaultValues();
        else
            LoadEditedValues();

        SavePreferences();
    }

    static void LoadDefaultValues()
    {
        var settingsDict = new NSDictionary(NSBundle.MainBundle.PathForResource("Settings.bundle/Root.plist", null));

        if (settingsDict != null)
        {
            var prefSpecifierArray = settingsDict[(NSString)"PreferenceSpecifiers"] as NSArray;

            if (prefSpecifierArray != null)
            {
                foreach (var prefItem in NSArray.FromArray<NSDictionary>(prefSpecifierArray))
                {
                    var key = prefItem[(NSString)"Key"] as NSString;

                    if (key == null)
                        continue;

                    var value = prefItem[(NSString)"DefaultValue"];

                    if (value == null)
                        continue;

                    switch (key.ToString())
                    {
                        case API_PATH_KEY:
                            ApiPath = value.ToString();
                            break;
                        default:
                            break;
                    }
                }
            }
        }
    }

    static void LoadEditedValues()
    {
        ApiPath = NSUserDefaults.StandardUserDefaults.StringForKey(API_PATH_KEY);
    }

    //Save new preferences to Settings
    static void SavePreferences()
    {
        var appDefaults = NSDictionary.FromObjectsAndKeys(new object[] {
            new NSString(ApiPath)
        }, new object[] {
            API_PATH_KEY
        });

        NSUserDefaults.StandardUserDefaults.RegisterDefaults(appDefaults);
        NSUserDefaults.StandardUserDefaults.Synchronize();
    }
}

SetUpByPreferences() ( ), .

+5

:

NSBundle.MainBundle.PathForResource ("Settings", @"bundle");
0

All Articles