Why hasAuthInKeychain() not always returns true while signing in a user with his google account to my Swift app?

Viewed 987

I'm following official google sign in tutorial from here https://developers.google.com/identity/sign-in/ios/start?ver=swift and I already implemented it, however I still face some basic errors. First of all - google suggest to do all that stuff in AppDelegate. So I put there all the necessary methods:

func application(application: UIApplication,
    didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {

        // Initialize sign-in
        var configureError: NSError?
        GGLContext.sharedInstance().configureWithError(&configureError)
        assert(configureError == nil, "Error configuring Google services: \(configureError)")

        GIDSignIn.sharedInstance().delegate = self

        return true
}

and the other function - since I'm using the backend server to authorize the user:

func signIn(signIn: GIDSignIn!, didSignInForUser user: GIDGoogleUser!,
    withError error: NSError!) {
        if (error != nil) {
            print("Looks like we have a sign in error: \(error)")
        }else {
            let tokenAsAParam = [
                "id_token":user.authentication.idToken
            ]

            NSNotificationCenter.defaultCenter().postNotificationName("ToggleAuthUINotification", object: nil, userInfo: ["statusText" : "Signed in user \(user)"])
            Alamofire.request(.POST, "http://mywebservice", parameters: tokenAsAParam)
                .response { (request, response, data, error) in
                    if(error != nil){
                        print("error during signing in with google= \(error)")
                        GIDSignIn.sharedInstance().signOut()
                    }else{

                        let sb = UIStoryboard(name: "Main", bundle: nil)
                        if let tabBarVC = sb.instantiateViewControllerWithIdentifier("TabController") as? TabController {
                            self.window!.rootViewController = tabBarVC

                        }

                    }
            }
            }
}

All that happens in AppDelegate.swift. But from there I'm not able to show user the popup to wait for the response from my webservice (before moving the user from ViewController to the main TabController), so so far the use case scenario is as follows:

1) user opens app and clicks sign in with google

2) user puts google credentials, clicks ok

3) user comes back to the page from point no. 1 (with google sign in button visible)

4) the app makes a call to webservice and in case of positive result it transfers user to the main TabController - when the internet connection is poor this takes some time and then user is confused because he is stuck for some time on the screen from point 1 and 3.

I would like to show some kind of indicator that the signing in process takes some time and user should wait. So in the code of ViewController I did:

override func viewWillAppear(animated: Bool) {

GIDSignIn.sharedInstance().uiDelegate = self

if(GIDSignIn.sharedInstance().hasAuthInKeychain()){
        loadingIndicator.startAnimating()
        print("we are here")
        someView.hidden = false //this is my view that contains "please wait sign"
        GIDSignIn.sharedInstance().signInSilently()
    }else{
        print("we are there")
        someView.hidden = true
    }

It almost works, but in some scenarios the GIDSignIn.sharedInstance().hasAuthInKeychain() has a value false even though the user is signed in.

For example:

when user turns the app and is already signed in - then he sees my please wait popup and after a short while he is redirected to the TabController. But when user is not signed in and clicks the google button sign in with google then - after putting credentials - the app comes back to my view controller with that button and I can see in the console: we are there - still, after a short while user goes to TabController, so seems like even though the GIDSignIn.sharedInstance().hasAuthInKeychain() got false - he was still able to log in. I think it might be a bug, but at this point I don't know yet. Do you know what might be wrong over here? Or maybe can you suggest me how to present a please wait message in that kind of scenario?

==========EDIT I just found out this - https://github.com/googlesamples/google-services/issues/27 it looks related to my problem, maybe someone from you guys had seen it before?

0 Answers
Related