问题
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.
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?
回答1:
Are you sure you have not user signed out or even disconnected somewhere?
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()
}
}
In case there is no authentication saved, you must trigger silent sign in and handle reply with your implementation of GIDSignInDelegate protocols didSignInForUser method.
回答2:
If, in your project, the class that implements GIDSignInUIDelegate is a subclass of UIViewController, then don't implement the
signInWillDispatch:error:
signIn:presentViewController:
signIn:dismissViewController:
methods of the GIDSignInUIDelegate protocol.
Remove these methods from your view controller if you added.
but you should conform the GIDSignInUIDelegate protocol without implementing the methods.
It will fix your error -4.
Please refer Google developers guide for assistance
来源:https://stackoverflow.com/questions/32517199/saving-the-current-gidgoogleuser-instead-of-signing-in-on-every-launch