locked
While in the PCL (shared code) how can I store prefs in NSUserDefaults, only for iOS? RRS feed

  • Question

  • User370480 posted

    At login, I am trying to store some user preferences only for iOS, in NSUserDefaults from the PCL project. I can check for plaform with device.iOS? And do I use dependency injection to trigger the ios specific code while in PCL? If this is correct how exactly do I use dependency injection and if im wrong what is the best way to do it?

    Friday, September 7, 2018 11:38 PM

All replies

  • User369979 posted

    If you just want to store some simple data in PCL. you can try to use Properties Dictionary.

    //For Saving Value
    Application.Current.Properties ["id "] ="123456";
    Application.Current.SavePropertiesAsync ();
    
    //For Retrieving Saved Value
    if (Application.Current.Properties.ContainsKey("id"))
    {
        var id = Application.Current.Properties ["id"] as string;
        // do something with id
    }
    

    You can also use dependency service to implement the data storage on each platform. NSUserDefaults is used on iOS, for Android try to use SharedPreferences. Moreover there're several libraries used for storing data for Xamarin.Forms. https://github.com/jamesmontemagno/SettingsPlugin this plugin will also be a good choice.

    Monday, September 10, 2018 2:48 AM