Sunday 11 October 2015

Using Xamarin.forms store value in Keystore (android) and Keychain (iOS)

This time I am going to share how I have used Platform Service  to store UserName and Password with Xamarin.forms. It will be quite a simple implementation as we will using Xamarin.Auth package.

First create a new Xamarin.forms application using pcl. I always prefer using pcl in my projects :).

In you pcl create a new interface as shown below:


public interface ILoginStoreService

    {

        void SaveLogin (string userName, string password);
        string GetUserName ();
        string GetPassword ();
        bool LoginExists ();

  }  


Then in your iOS project add Xamarin.Auth package. After that inside the same iOS project create a new Class inheriting the interface defined in the pcl project.

[assembly: Dependency(typeof(LoginStoreIOS))]
namespace HealthGateway.iOS
{   
        public class LoginStoreIOS        
            :ILoginStoreService    
       {        
                public void SaveLogin (string userName, string password)        
               {            
                        if (!string.IsNullOrEmpty (userName) && !string.IsNullOrEmpty (password)) 
                        {               
                                  Account user = new Account { Username = userName };                
                                  user.Properties.Add ("Key", password);                
                                 AccountStore.Create ().Save (user, "Shribits");            
                        }        
              }      
             public string GetPassword ()       
            {            
                       var account = AccountStore.Create ().FindAccountsForService ("Shribits").Last();           
                       return account.Properties["Key"];        
            }
            public bool LoginExists ()        
          {            
                    if(AccountStore.Create ().FindAccountsForService ("Shribits").Count() > 0)               
                             return true;            
                    else                
                             return false;        
           }       
           public string GetUserName ()        
           {            
                   var account = AccountStore.Create ().FindAccountsForService ("Shribits").Last();            
                  return account.Username;        
           }    
    }
}  

Similarly for android follow the same steps create a new Class inheriting the interface defined in your pcl and also add the Xamarin.Auth package to your solution.

The example of implementation is below:


[assembly: Dependency(typeof(LoginStoreDroid))]

namespace HealthGateway.Droid

{
    public class LoginStoreDroid
        :ILoginStoreService
    {
        public void SaveLogin (string userName, string password)
        {
            if (!string.IsNullOrEmpty (userName) && !string.IsNullOrEmpty (password)) {
                Account user = new Account { Username = userName };
                user.Properties.Add ("Key", password);
                AccountStore.Create (MainActivity.Instance.BaseContext).Save (user, "Shribits");
            }
        }
        public string GetPassword ()
        {
            var account = AccountStore.Create (MainActivity.Instance.BaseContext).FindAccountsForService ("Gateway").Last();
            return account.Properties["Key"];
        }
        public bool LoginExists ()
        {
            if(AccountStore.Create (MainActivity.Instance.BaseContext).FindAccountsForService ("Shribits").Count()>0)
                return true;
            else
                return false;
        }
        public string GetUserName ()
        {
            var account = AccountStore.Create (MainActivity.Instance.BaseContext).FindAccountsForService ("Gateway").Last();
            return account.Username;
        }
    }
}




Now you are good to go ahead with your implementation cross platform level. Below is the example of how to make the call :

DependencyService.Get<ILoginStoreService> ().LoginExists ()  

For more information on how to implement Dependency Service please https://developer.xamarin.com/guides/cross-platform/xamarin-forms/dependency-service/

Add in your comments and questions. Also let me know if you would like to cover any topic.