How can we store values i.e., username and password in local database in xamarin.forms.
I am using Write device platform specific code in Xamarin.Forms
How can we store values i.e., username and password in local database in xamarin.forms.
I am using Write device platform specific code in Xamarin.Forms
I have done this before using dependency injection.
First create an interface that you will implement in your android and iOS projects
public interface UserInterface
{
void saveUserName(string userName);
string loadUserName();
}
Then implement that interface in both android and iOS projects
Android:
[assembly: Xamarin.Forms.Dependency (typeof (ChessUser_Android))]
public class ChessUser_Android : Java.Lang.Object, UserInterface {
public void saveUserName(string userName){
var prefs = Application.Context.GetSharedPreferences("MyApp", FileCreationMode.Private);
var prefEditor = prefs.Edit();
//save user's properties for a specific key
prefEditor.PutString("user_name",userName);
prefEditor.Commit();
}
public string loadUserName(){
var prefs = Application.Context.GetSharedPreferences("MyApp", FileCreationMode.Private);
return prefs.GetString("user_name",null);
}
}
iOS
[assembly: Xamarin.Forms.Dependency (typeof (ChessUser_iOS))]
public class ChessUser_iOS : UserInterface {
public void saveUserName(string userName){
var prefs = NSUserDefaults.StandardUserDefaults;
//save user's properties for a specific keys
prefs.SetValueForKey (new NSString (userName), new NSString ("user_name"));
}
public string loadUserName(){
var prefs = NSUserDefaults.StandardUserDefaults;
return prefs.StringForKey ("username");
}
}
when I want to save or load data i.e., username and password
DependencyService.Get<UserInterface>().loadUserName();
DependencyService.Get<UserInterface>().saveUserName("name");
While full SQLite support shown in the other answers are all great, here's an alternative if you just need simple key value storage (ex: username and passwords). It abstracts NSUserDefaults (iOS) and SharedPreferences (Android).
Shared Project
public interface ISimpleStorage
{
void Set(string key, string value);
string Get(string key);
void Delete(string key);
}
iOS Project
[assembly: Dependency(typeof(SimpleStorage))]
namespace MyApp.iOS
{
public class SimpleStorage : ISimpleStorage
{
public void Set (string key, string value)
{
NSString newKey = new NSString (key);
NSString newValue = new NSString (value);
NSUserDefaults.StandardUserDefaults.SetValueForKey (newValue, newKey);
}
public string Get (string key)
{
NSString newKey = new NSString (key);
NSString value = (NSString)NSUserDefaults.StandardUserDefaults.ValueForKey (newKey);
return value != null ? value.ToString () : null;
}
public void Delete (string key)
{
NSString newKey = new NSString (key);
NSUserDefaults.StandardUserDefaults.RemoveObject (newKey);
}
}
}
Android
[assembly: Dependency(typeof(SimpleStorage))]
namespace MyApp.Android
{
public class SimpleStorage : ISimpleStorage
{
public void Set (string key, string value)
{
var prefs = Forms.Context.GetSharedPreferences("MyApp", FileCreationMode.Private);
var prefEditor = prefs.Edit();
prefEditor.PutString (key, value);
prefEditor.Commit();
}
public string Get (string key)
{
var prefs = Forms.Context.GetSharedPreferences("MyApp", FileCreationMode.Private);
return prefs.GetString(key, null);
}
public void Delete (string key)
{
var prefs = Forms.Context.GetSharedPreferences("MyApp", FileCreationMode.Private);
var prefEditor = prefs.Edit ();
prefEditor.Remove (key);
prefEditor.Commit();
}
}
}
I then have a helper class in my Shared project that looks like this:
public static class Device
{
public static ISimpleStorage Storage { get { return DependencyService.Get<ISimpleStorage> ();}}
}
I then can use this throughout my shared code as follows:
var username = Device.Storage.Get ("username");
Device.Storage.Set ("username", textEntry.Text);
Device.Storage.Delete ("username");