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.

11 comments:

  1. This comment has been removed by a blog administrator.

    ReplyDelete
  2. This comment has been removed by the author.

    ReplyDelete
    Replies
    1. Have you uploaded this to github?

      Delete
    2. Let me know if you are have any difficulty in understanding the code above.
      I can help you out and upload a sample on github

      Delete
    3. please upload a sample. i have issues

      Delete
  3. Hi, I wrote same code, but value is gone when app's uninstalled.
    It's alive on iOS. But it's gone on Android.

    Don't you have same experience?

    ReplyDelete
  4. I have the same experience too

    ReplyDelete
  5. MainActivity.Instance giving error in Android.
    It says MainActivity does not contain definition for Instance.

    ReplyDelete
  6. Solved it using this approach:

    https://forums.xamarin.com/discussion/46032/andriod-service-activity

    ReplyDelete
  7. It is always a good idea to have a flashlight stored in your car or house in case of keychain supplier singapore The strength of promotional products lies on the fact that they are practical items that will be very useful to your intended recipient. Nowadays, businesses are looking for an alternative medium for promoting their business. Using billboards and other conventional forms of advertising has become too expensive for companies with limited advertising budget.

    ReplyDelete