Xamarin.Auth:更新应用时,帐户数据不会保留

时间:2013-11-07 10:01:48

标签: c# xamarin.ios xamarin.android xamarin xamarin.auth

我正在使用Xamarin.Auth对foursquare等服务进行身份验证。我的身份验证代码运行良好,问题是当我部署新版本的应用程序时,帐户数据不会保留 - 每次我在手机上部署测试版本时,我都要重新进行身份验证。

以下是我存储帐户的方式;

  /// <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");
}

以下是我检查是否有存储帐户的方法;

 /// <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;
    }
}

1 个答案:

答案 0 :(得分:4)

默认行为是您的Android设置每次部署应用时都会重置。为了防止这种情况,在Xamarin Studio中 - &gt;偏好 - &gt; Android - &gt;选中“在应用程序部署之间保留数据/缓存”

如果您使用的是Visual Studio,则应该有类似的设置。