保存当前的GIDGoogleUser,而不是在每次启动时login

我正在使用GIDSignInButton将我的用户签入Google。 问题是,我不知道如何保存当前用户,以便每个用户不必每次打开应用程序login。 我试过使用signInSilently()但我得到The operation couldn't be completed. (com.google.GIDSignIn error -4.) 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, 

在我的情况下,用户已经login给定的范围,他们还没有退出。 所以我不确定是什么原因导致了这个错误。

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

你确定你没有用户退出,甚至连接断开?

我总是检查用户是否已经login或使用hasAuthInKeychain保存了以前的身份validation(例如在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() } } 

如果没有保存authentication,则必须触发沉默login并处理GIDSignInDelegate协议didSignInForUser方法的实现回复。

如果在你的项目中,实现GIDSignInUIDelegate的类是UIViewController的子类,那么不要实现

 signInWillDispatch:error: signIn:presentViewController: signIn:dismissViewController: 

GIDSignInUIDelegate协议的方法。

如果添加,从视图控制器中删除这些方法。 但是您应该遵守GIDSignInUIDelegate协议而不实施这些方法。

它会解决你的错误-4。

请参阅Google开发人员指南获取帮助