我正在使用 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/