GIDSignIn.sharedInstance().currentUser nil when relaunching app

末鹿安然 提交于 2019-12-06 12:08:05

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 call getApplicationData(dataApi) to populate data.

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.

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
    }
    }}
    
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!