保存当前的GIDGoogleUser,而不是每次启动时都登录 [英] Saving the current GIDGoogleUser instead of signing in on every launch

查看:118
本文介绍了保存当前的GIDGoogleUser,而不是每次启动时都登录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用GIDSignInButton将我的用户登录到Google.问题是,我不确定如何保存当前用户,以免每个用户每次打开应用程序时都无需登录.我尝试使用signInSilently(),但每次都会得到The operation couldn’t be completed. (com.google.GIDSignIn error -4.).

I'm using the GIDSignInButton to sign my users into Google. Problem is, I'm not sure how to save the current user so that each user doesn't have to sign in every time they open the app. I've tried using signInSilently() but I get The operation couldn’t be completed. (com.google.GIDSignIn error -4.) every time.

该错误在头文件中这样说:

That error, in the header file, says this :

// 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,

在我的情况下,用户已经使用给定的范围登录,而他们尚未退出.因此,我不确定是什么原因导致了该错误.

In my case, the user has already signed in with the given scopes, and they have not signed out yet. So I'm not sure what could be causing that error.

用户登录后,如何保存该实例之后可以使用signInSilently()的位置?刷新和访问令牌也涉及处理吗?

After a user signs in, how should I save that instance where I'm able to use signInSilently() after that? Is there handling involved with the refresh and access tokens as well?

推荐答案

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

Are you sure you have not user signed out or even disconnected somewhere?

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

I'm always checking whether the user has either currently signed in or has previous authentication saved in using hasAuthInKeychain (e.g. 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()
    }
}

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

In case there is no authentication saved, you must trigger silent sign in and handle reply with your implementation of GIDSignInDelegate protocols didSignInForUser method.

这篇关于保存当前的GIDGoogleUser,而不是每次启动时都登录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆