简体   繁体   中英

Swift + GIDSignIn - hasAuthInKeyChain returns true but currentUser is still nil after calling signInSilently

I'm attempting to integrate Google sign-in into my app. Right now, I'm able to successfully log in a user with a Google account and the app works as expected - I'm able to access currentUser , get some information from it, and fill my app's views with the information. The problem is that when I relaunch the app, I'm unable to get a GIDGoogleUser even after calling signInSilently . hasAuthInKeychain returns true, so I know that an auth token must exist in the keychain. I just can't figure out why currentUser still returns nil after signing a user back into the app. The code I've written is below:

    guard let signIn = GIDSignIn.sharedInstance() else { fatalError() }
    signIn.scopes = ["https://www.googleapis.com/auth/plus.stream.read", "https://www.googleapis.com/auth/plus.me", "https://www.googleapis.com/auth/plus.login"]
    signIn.clientID = FIRApp.defaultApp()?.options.clientID
    signIn.delegate = self
    if signIn.hasAuthInKeychain() {
        signIn.signInSilently()
    }

    if signIn.hasAuthInKeychain() || FBSDKAccessToken.current() != nil {

        var token = String()
        if let user = signIn.currentUser{
            token = user.userID
        } else{
            token = FBSDKAccessToken.current().userID
        }
    }

The app tries to sign in silently since hasAuthInKeychain returns true. But it skips past the if let statement because currentUser is nil and goes directly into trying to find a FBSDKAccessToken . I've tried all the suggestions I've found including setting the sign-in scopes. Am I missing something?

I had a similar issue. First I thought it was a timing issue. By invoking signIn.signInSilently() after fully loading the root viewController it did work. I am now 99% sure I solved it by executing signIn.signInSilently() on the main thread through:

DispatchQueue.main.async {
    gidSignIn.signInSilently()
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM