保存当前的GIDGoogleUser,而不是每次启动时都登录

chi*_*uda 17 ios google-oauth swift

我正在用GIDSignInButton我的用户签到谷歌.问题是,我不确定如何保存当前用户,以便每个用户每次打开应用程序时都不必登录.我尝试过使用signInSilently()The operation couldn’t be completed. (com.google.GIDSignIn error -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,
Run Code Online (Sandbox Code Playgroud)

就我而言,用户已经使用给定的范围登录,但他们还没有退出.所以我不确定是什么导致了这个错误.

用户登录后,如何将该实例保存到我signInSilently()之后可以使用的位置?是否还涉及刷新和访问令牌?

Vol*_*bet 3

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

我总是检查用户当前是否已登录或使用hasAuthInKeychain(例如 in 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()
    }
}
Run Code Online (Sandbox Code Playgroud)

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