ios - 保存当前 GIDGoogleUser 而不是在每次启动时登录

标签 ios swift google-oauth

我正在使用 GIDSignInButton 让我的用户登录 Google。问题是,我不确定如何保存当前用户,这样每个用户就不必在每次打开应用程序时都登录。我试过使用 signInSilently() 但我得到 The operation couldn't be completed。 (com.google.GIDSignIn 错误 -4。) 每次。

头文件中的那个错误是这样说的:

// Indicates there are no auth tokens in the keychain. This error code will be returned by
  // signInSilently if the user has never signed in before with the given scopes, or if they have
  // since signed out.
  kGIDSignInErrorCodeHasNoAuthInKeychain = -4,

在我的例子中,用户已经使用给定的范围登录,并且他们还没有退出。所以我不确定是什么导致了这个错误。

用户登录后,我应该如何保存那个我可以在之后使用 signInSilently() 的实例?是否也涉及刷新和访问 token 的处理?

最佳答案

您确定您没有在某处注销或什至断开连接吗?

我一直在使用 hasAuthInKeychain(例如,在 viewWillAppear 中)检查用户当前是否已登录或是否保存了以前的身份验证:

private func checkIfGoogleUserIsAuthorized() {
    if GIDSignIn.sharedInstance().hasAuthInKeychain() {
        // User was previously authenticated to Google. Attempt to sign in.
        GIDSignIn.sharedInstance().signInSilently()
    } else {
        // User was not previously authenticated to Google.
        self.updateUI()
    }
}

如果没有保存身份验证,您必须触发静默登录并通过实现GIDSignInDelegate 协议(protocol)didSignInForUser 方法来处理回复。

关于ios - 保存当前 GIDGoogleUser 而不是在每次启动时登录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32517199/

相关文章:

ios - 异步 Web 请求后无法让事件指示器停止

swift - 为什么我必须以键值形式将参数传递给类?

node.js - YouTube : Error, 需要登录吗?

ios - 在 Realm 中存储来自 API 的数据

ios - NSInvalidArgumentException',原因: 'Unable to parse the format string "userName like %@ password like % @"'

ios - 在半圆上绘制比例尺 Swift/UIView

java - 在java中使用OAuth 2.0服务帐户的Google电子邮件设置api

google-api - oauth2client.client - 收到没有 refresh_token 的 token 响应。考虑使用提示 ='consent' 重新验证

ios - 这种方法是否总是给我 12 :00:00 of the current day in ios?

ios - xcode 4 : how can I add a custom compiler?