'why my google sign in doesn't show account selection after I successfully sign in?

I have successfully implemented a google sign in my app that uses Firebase. when I first sign in using google account a dialog to choose the google account will appear.

but after I successfully login and then log out, when I try to log in again, that dialog to choose google account doesn't appear again, it just directly send me to the main menu.

I want to always show that dialog every time the user sign in via google. here is the code I use, in my VC that has google sign in button

import UIKit
import Firebase
import GoogleSignIn
import SVProgressHUD

class AuthenticationVC : UIViewController, GIDSignInUIDelegate, GIDSignInDelegate {

    var userID : String?

    override func viewDidLoad() {
        super.viewDidLoad()

        // delegate declaration
        GIDSignIn.sharedInstance().uiDelegate = self

        // For Google SignIn Using Firebase
        GIDSignIn.sharedInstance().clientID = FirebaseApp.app()?.options.clientID
        GIDSignIn.sharedInstance().delegate = self
    }




    @IBAction func googleButtonDidPressed(_ sender: Any) {
        GIDSignIn.sharedInstance().signIn()
    }



}


extension AuthenticationVC {

    // MARK: - Firebase Google Sign In Authentication Methods
    // The methods below will be triggered if the user want to login via google account

    @available(iOS 9.0, *)
    func application(_ app: UIApplication, open url: URL, options: [UIApplicationOpenURLOptionsKey : Any] = [:]) -> Bool {
        return GIDSignIn.sharedInstance().handle(url,sourceApplication:options[UIApplicationOpenURLOptionsKey.sourceApplication] as? String, annotation: [:])
    }



    func application(_ application: UIApplication, open url: URL, sourceApplication: String?, annotation: Any) -> Bool {
        return GIDSignIn.sharedInstance().handle(url,sourceApplication: sourceApplication,annotation: annotation)
    }


    func sign(_ signIn: GIDSignIn!, didSignInFor user: GIDGoogleUser!, withError error: Error?) {

        SVProgressHUD.show(withStatus: "Harap Tunggu")

        if let error = error {
            SVProgressHUD.dismiss()
            print("failed to login into google")
            print("\(error.localizedDescription)")
            return
        }
        print("user successfully signin into google")

        guard let authentication = user.authentication else {
            SVProgressHUD.dismiss()
            return
        }

        let credential = GoogleAuthProvider.credential(withIDToken: authentication.idToken,
                                                       accessToken: authentication.accessToken)

        Auth.auth().signIn(with: credential) { (user, error) in
            if let error = error {
                SVProgressHUD.dismiss()
                print("failed to create firebase user using google account")
                print("\(error.localizedDescription)")
                return
            }

            print("user successfully logged into firebase")

            guard let userKMFromGoogleSignIn = user else {
                SVProgressHUD.dismiss()
                return

            }

            self.userID = userKMFromGoogleSignIn.uid


            // if the user sign Up for the very first time using google account, then user Basic Information stored in the firestore  will be nil, therefore we create user basic data to firestore

            // if user has signed up before via google account, then directly send to mainTabBar (it means he/she has picked a city before), otherwise pickCity first

            self.checkUserBasicInformationInFirestore(userKMFromGoogleSignIn)


        }

    }

    func sign(_ signIn: GIDSignIn!, didDisconnectWith user: GIDGoogleUser!, withError error: Error!) {
        // Perform any operations when the user disconnects from app here.
        // ...
    }


}

and here is the code when the user taps the logout button

  @IBAction func logOutButtonDidPressed(_ sender: Any) {
        do {
            try Auth.auth().signOut()
            print("User Did Log Out")
            performSegue(withIdentifier: "Back", sender: nil)
        } catch {
            showAlert(alertTitle: "Sorry", alertMessage: "\(error)", actionTitle: "Back")
        }
    }

what should I do?



Solution 1:[1]

Try signing out when entering your view controller. Signing out will expire the current token and you will be asked to sign in again with list of accounts.

override func viewDidLoad() {
    //Configure and set delegate
    ...
    GIDSignIn.sharedInstance().signOut()
    GIDSignIn.sharedInstance().disconnect()
}

@IBAction func logOutButtonPressed(_ sender: Any) {
    GIDSignIn.sharedInstance().signOut()
    GIDSignIn.sharedInstance().disconnect()
}

Solution 2:[2]

While clicking Google Login Button, we should remove previous shared instance.

    @IBAction func googleLoginAction(_ sender: Any) {
            signOut()
            let sighIn:GIDSignIn = GIDSignIn.sharedInstance()
            sighIn.delegate = self;
            sighIn.uiDelegate = self;
            sighIn.shouldFetchBasicProfile = true
            sighIn.scopes = ["https://www.googleapis.com/auth/plus.login","https://www.googleapis.com/auth/userinfo.email","https://www.googleapis.com/auth/userinfo.profile","https://www.googleapis.com/auth/plus.me"];
            sighIn.clientID = ""
            sighIn.signIn()
        }

    func signOut() -> Void {
            GIDSignIn.sharedInstance().signOut()
        }

func sign(inWillDispatch signIn: GIDSignIn!, error: Error!) {

    }
    func sign(_ signIn: GIDSignIn!, present viewController: UIViewController!) {
        self.present(viewController, animated: true, completion: nil)
    }
    func sign(_ signIn: GIDSignIn!, dismiss viewController: UIViewController!) {
        self.dismiss(animated: true, completion: nil)
    }
func sign(_ signIn: GIDSignIn!, didSignInFor user: GIDGoogleUser!, withError error: Error!) {
        if (error != nil) {
            return
        }
        reportAuthStatus()

        socialId = user.userID
        socialMail = user.profile.email
        socialFirstName = user.profile.givenName
        socialLastName = user.profile.familyName

        socialLoginFunction(socialName: "google", socialID: self.socialId)

    }
    func sign(_ signIn: GIDSignIn!, didDisconnectWith user: GIDGoogleUser!, withError error: Error!) {
        if (error != nil) {

        } else {

        }
    }

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2 iOS Geek