0

In the B Viewcontroller, I store data into NSUserDefaults and retrieve them in AViewController.

During the execution, it performs as it expected. However, once I close and re-run the app, I figure out that these information (dictionary) does not being stored.

Here is the BViewController where I store the data in NSUserDefaults

partial class BViewController : UIViewController
{
    const string server = "server";
    const string port = "port";
    const string password = "password";
    const string username = "username";
    const string inboxuserid = "inboxuserid";
    .............
    .............



  public override void ViewWillDisappear (bool animated)
  {
    var appDefaults = new NSDictionary (server, serverTF.Text, port, portTF.Text, password, passwordTF.Text,username,usernameTF.Text, inboxuserid,inboxuserTF.Text);
    NSUserDefaults.StandardUserDefaults.RegisterDefaults (appDefaults);
    NSUserDefaults.StandardUserDefaults.Synchronize ();
  }

}

and retrieve them in the A ViewController as follows:

partial class AViewController : UIViewController
 {
  public override void ViewWillAppear(bool animated)
   {
    var username = NSUserDefaults.StandardUserDefaults.StringForKey ("username");
    Console.WriteLine (username);
    }
}
  • 1
    you shouldn't call RegisterDefaults or Synchronize in your A controller. – Jason Feb 16 '15 at 17:27
  • @Jason, what should I call? How should I pass nsdictionary to the nsuserdefaults? –  Feb 16 '15 at 17:29
  • You're already calling RegisterDefaults in B Controller. You don't need to do this multiple times. – Jason Feb 16 '15 at 17:33
  • Ok, I have fixed the way that you have pointed, still same issue. I think my problem mostly is about nsdictionary in nsuserdefaults. because I have tested with a simple string and simple strings stored and retrieved after the app is reopened. I could store each string separately, however using nsdictionary makes much better shape. –  Feb 16 '15 at 17:39

1 Answers1

2

Based on the following article: https://moovendan.wordpress.com/2012/12/26/monotouch-nsuserdefaults-store-values-in-settings/

I would propose the following code, I have tested with a small example and it worked.

BViewController, store the dictionary in the NSUserDefaults:

NSDictionary appDefaults= new NSDictionary(server, serverTF.Text, port, portTF.Text, password, passwordTF.Text, username, usernameTF.Text, inboxuserid, inboxuserTF.Text);
NSString key = new NSString ("dict");
NSUserDefaults.StandardUserDefaults.SetValueForKey (appDefaults, key);

AViewController, retrive the dictionary from NSUserDefaults:

NSString key = new NSString ("dict");
NSDictionary d2 = NSUserDefaults.StandardUserDefaults.DictionaryForKey (key);
Console.WriteLine (d2["username"]);
casillas
  • 16,351
  • 19
  • 115
  • 215
  • ok your code works. However I would like to know what is the main difference from the previous solution. Could you please elaborate ? –  Feb 16 '15 at 18:00
  • 1
    check the following answer http://stackoverflow.com/questions/9166468/why-use-registerdefaults-instead-of-setvalueforkey – casillas Feb 16 '15 at 20:04
  • 1
    Even this answer: http://stackoverflow.com/questions/4931167/what-is-the-use-of-nsuserdefaults-registerdefaults – casillas Feb 16 '15 at 20:08