2014-01-08 36 views
4

我一直在嘗試很長一段時間才能找到解決方案,應該是一個非常簡單的問題。 我想要的是在應用程序未運行時可以在iPhone設置菜單中編輯的變量。基本上是一個包裝在iOS GUI中的配置文件。訪問Xamarin.iOS Settings.Bundle?

這應該是iOS中的內置功能,雖然我可以找到一些與之相關的方法,但我找不到實際的解決方案。

最近我一直想得到我想要的東西就像其他變量一樣工作:在應用程序啓動時清空,並在應用程序關閉時再次被擦傷。並且在iPhone設置窗口中仍然不可見。

這是我的代碼有:

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's 'On' state to the value. Currently it always returns False. 
    bool saveCredentials = _thisUser.BoolForKey("SaveCredentials"); 
    chckBoxRememberMe.On = saveCredentials; 
} 

而且我Settings.Bundle 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>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> 

任何人在那裏是誰在瞎與Xamarin iOS和知道如何工作的?

回答

5

編輯:這裏是Xamarin工作項目:https://github.com/xamarin/monotouch-samples/tree/master/AppPrefs

首先,你需要使你的觀點在iPhone設置窗口中可見。

爲此,您需要在應用程序包的頂級目錄中創建一個名爲「Settings.bundle」的文件夾。然後,創建名爲「Root.plist」的新文件。文件必須是類型屬性列表的。 您可以通過右鍵點擊Settings.bundle,然後添加 - >新建文件... - > iOS(在左側窗格) - >屬性列表。如果您添加一個空文件,然後將其重命名爲.plist,則不會在iPhone的設置中顯示。

你的Root.plist中的第一個元素必須是一個數組,並且它必須包含字典。

你可以在這裏找到更多的信息如何構建你的設置查看: https://developer.apple.com/library/ios/DOCUMENTATION/Cocoa/Conceptual/UserDefaults/Preferences/Preferences.html

注重圖4-2(字符串文件名是沒有必要的)。另外,在Xamarin編輯器中編輯Root.plist文件,它更容易。

要新創建的設置得到的值,你可以使用這個類(只要你想添加儘可能多的屬性):

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()(這是唯一的公共方法),然後從類中的屬性獲取值。

0

試試這個:

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