11

Would you guys help me? I found how to save and retrieve simple objects to use them as app settings.

NSUserDefaults.StandardUserDefaults.SetString("testUser","username");
NSUserDefaults.StandardUserDefaults.Init(); //Although it's strange that you have to call this method to actually save your stuff

and later you can retrieve it like that:

var username = NSUserDefaults.StandardUserDefaults.StringForKey("username");

Now, what if I need to save multiple usernames (as many as I want)? And what if I need to save multiple usernames with passwords or passhashes?

iLemming
  • 34,477
  • 60
  • 195
  • 309

3 Answers3

12

Here's an example for arrays (many strings kept with a single key):

        NSUserDefaults.StandardUserDefaults ["array"] = NSArray.FromStrings ("a", "b", "c");
        foreach (string s in NSUserDefaults.StandardUserDefaults.StringArrayForKey ("array")) {
            Console.WriteLine (s);
        }

And an example where a dictionary (key=value) is kept with a single key:

        NSDictionary dict = NSDictionary.FromObjectsAndKeys (new object[] { "user1", "user2" }, new object[] { "123", "abc" });
        NSString key = new NSString ("dict");
        NSUserDefaults.StandardUserDefaults.SetValueForKey (dict, key);
        NSDictionary d2 = NSUserDefaults.StandardUserDefaults.DictionaryForKey (key);
        for (int i = 0; i < d2.Count; i++) {
            Console.WriteLine ("{0} : {1}", d2.Keys [i], d2.Values [i]);
        }
poupou
  • 43,413
  • 6
  • 77
  • 174
1

I would consider using core-data if you have a lot of users you also want to search for and filter. If your app is not that complex, you can always serialize a dictionary into a string and save it as such in the user defaults. A good library for JSON (de)serialization is SBJSON http://stig.github.com/json-framework/.

huesforalice
  • 2,418
  • 2
  • 24
  • 29
  • I thought about serializing stuff, but I can't turn my object into NSObject by using NSObject.FromObject()... it returns null... – iLemming Oct 05 '11 at 17:05
  • Are you using sbjson? If so, you can import SBJSON.h. Your dictionary then has a new method jsonRepresentation which gives you the serialized String. This String you can then save. If you get the String back from the userdefaults, you can call JSONValue on the string and it'll return a dictionary. – huesforalice Oct 05 '11 at 17:08
  • No I'm trying to use NSJsonSerialization.Serialize it throws an exception. I haven't learn yet how to utilize objective-c libraries in MonoTouch projects... It seems I have to right now – iLemming Oct 05 '11 at 17:28
1

I had trouble getting NSUserDefaults to store dictionaries. What I ended up doing, is simply obtaining the Documents directory, and saving my dictionary to a file when the app is terminating or going to the background, then loading it from a file when the app finishes loading. NSDictionary and NSMutableDictionary have class methods that load or save from a file - and it uses serialized XMLs, so it's nice and elegant.

This is good if your app doesn't need a large amount of data, and the data is simple and structure is fluid.

Jay Imerman
  • 4,475
  • 6
  • 40
  • 55