3

I'm using Xamarin.Auth to authenticate with services like foursquare and so. I've gotten my authentication code working all good, the problem is that the account data is not persisted when I deploy a new version of my app - each time I deploy a test version on the phone, I've to re-authenticate.

Here's how I store the account;

  /// <summary>
/// Stores the account.
/// </summary>
private void StoreAccount(AuthenticatorCompletedEventArgs eventArgs)
{
    if (!eventArgs.IsAuthenticated) // make sure we are authenticated.
    {
        Log.Debug(Logging.AppTag, "FourSquareClient can't store account as auth. is cancelled. ");
        return;
    }

    this.IsAuthenticated = true;
    this.Account = eventArgs.Account;

    AccountStore.Create(this.OwnerContext).Save(eventArgs.Account, "Foursquare");
}

and here is how I check if we have a stored account;

 /// <summary>
/// Retrieves stored account.
/// </summary>
private void RetrieveAccount()
{
    if (this.IsAuthenticated)
    {
        Log.Debug(Logging.AppTag, "FourSquareClient is already authenticated! ");
        return;
    }


    var accounts = AccountStore.Create(this.OwnerContext).FindAccountsForService("Foursquare");
    var enumerable = accounts as IList<Account> ?? accounts.ToList();

    if (enumerable.Any())
    {
        Log.Info(Logging.AppTag, "Foursquareclient found account data.");
        this.IsAuthenticated = true;
        this.Account = enumerable.First();
    }
    else
    {
        Log.Info(Logging.AppTag, "Foursquareclient no account data found!");
        this.IsAuthenticated = false;
        this.Account = null;
    }
}
HuseyinUslu
  • 4,094
  • 5
  • 33
  • 50

1 Answers1

4

The default behavior is for your Android settings to get reset every time the app is deployed. To prevent this, in Xamarin Studio --> Preferences --> Android --> check "Preserve data/cache between application deploys"

There should be a similar setting if you are using Visual Studio.

Jason
  • 86,222
  • 15
  • 131
  • 146
  • I could not find the similar settings in Visual Studio. I would appreciate if anyone could help me find it. – To Ka Sep 09 '14 at 14:30
  • 5
    Tools -> Options -> Xamarin -> Android Settings -> Preserve Application Data – Jason Sep 09 '14 at 14:55