问题
Initially I was having a problem with the check to see if we have saved data into the keychain. I found this post and followed the suggestion by BhavinBhadani:
Whenever you check if ([GIDSignIn sharedInstance].hasAuthInKeychain) , before that add your sign in scopes..
https://github.com/googlesamples/google-services/issues/27
This has solved my first problem of GIDSignIn.sharedInstance().hasAuthInKeychain()
returning true after the user has logged in successfully before and then has killed app and re runs.
However now I am doing the following:
let signIn = GIDSignIn.sharedInstance()
signIn.scopes = [Constants.GOOGLE_CLIENT_SCOPE, Constants.GOOGLE_OIDC_SCOPE]
if GIDSignIn.sharedInstance().hasAuthInKeychain() {
print("We have saved in keychain")
if let u = GIDSignIn.sharedInstance().currentUser {
getData(dataApi)
}
else {
// THIS CODE IS BEING SUCCESSFULLY HIT
GIDSignIn.sharedInstance().signInSilently()
getApplicationData(dataApi)
}
}
However even after doing: GIDSignIn.sharedInstance().signInSilently()
the GIDSignIn.sharedInstance().currentUser
is nil.
Has anyone successfully used signInSilently()
to populate the currentUser data again?
回答1:
I make a point in that issue in git that you also need to add scopes before check hasAuthInKeychain
like this .. this works for me and hope same for you
let signIn = GIDSignIn.sharedInstance()
signIn.scopes = ["https://www.googleapis.com/auth/plus.login","https://www.googleapis.com/auth/plus.me"]
if (signin.hasAuthInKeychain) {
print("Signed in");
} else {
print("Not signed in");
}
make sure after
GIDSignIn.sharedInstance().signInSilently()
, there is some delay to callgetApplicationData(dataApi)
to populate data.
回答2:
Have you tried the the after deleting the app. It may possible at the time of login you have not added the Keychain sharing capability to the app. and now when you try to get
if GIDSignIn.sharedInstance().hasAuthInKeychain()
it returns true but current user is still nil.
I had faced the same issue, so deleted the app and did the login again. Now I can see the current user.
回答3:
For me, the trick was enabling 'Keychain Sharing' in Capabilities.
So my receipt is:
Set scopes
'hasAuthInKeychain' check
- True means we can 'signInSilently', otherwise normal signIn
If we check 'currentUser' just afterward it will be nil, the right place to get it is in GIDSignInDelegate method - didSignInFor user:
func authCheck() { let signIn = GIDSignIn.sharedInstance() signIn?.scopes = ["https://www.googleapis.com/auth/calendar"] if GIDSignIn.sharedInstance().hasAuthInKeychain() { // The user has either currently signed in or has previous authentication saved in keychain. GIDSignIn.sharedInstance().signInSilently() } else { showLoginScene() }} func sign(_ signIn: GIDSignIn!, didSignInFor user: GIDGoogleUser!, withError error: Error!) { if let error = error { print("\(error.localizedDescription)") } else { // Now currentUser has value guard let currentUser = GIDSignIn.sharedInstance().currentUser else { print("currentUser is nil") return nil } }}
回答4:
Recently Google has changed the methods for checking if the user has been already loggedIn in Google SignIn SDK v5.0.0.
Update calls to signInSilently and hasAuthInKeychain to restorePreviousSignIn and hasPreviousSignIn.
So the code will look like
if(GIDSignIn.sharedInstance().hasPreviousSignIn()) {
GIDSignIn.sharedInstance()?.restorePreviousSignIn()
} else {
navigateToLogin()
}
Migration guide: https://developers.google.com/identity/sign-in/ios/quick-migration-guide
来源:https://stackoverflow.com/questions/39830457/gidsignin-sharedinstance-currentuser-nil-when-relaunching-app