0

The segue needs to take place only after the user has been authenticated with their Google account. This authentication process takes place in the AppDelegate. I have a segue between two view controllers rather than a button so when the button is tapped, the segue will only be called for once.

The following code takes place within the sign function in the AppDelegate for anyone who is familiar with Firebase:

Auth.auth().signIn(with: credential) { (authResult, error) in
            if let error = error {
                print("Firebase sign In error")
                print(error)
                return
            } else {
                let db = Firestore.firestore()

                db.collection("users").getDocuments() { (querySnapshot, err) in
                    if let err = err {
                        print("Error getting documents: \(err)")
                    } else {
                        print("Accessing email")
                        for document in querySnapshot!.documents {
                            if document.get("uid") as? String != Auth.auth().currentUser?.uid {
                                db.collection("users").addDocument(data: ["firstName": firstName!, "lastName": lastName!, "email": email!, "uid": authResult!.user.uid]) { (error) in
                                    if error != nil {
                                        print("Error: User data not saved")
                                        return
                                    }
                                }
                            }
                        }
                    }
                }
            self.window?.rootViewController!.performSegue(withIdentifier: "googleSegue", sender: nil)
            print("User is signed in with Firebase")

        }
    }
}

However the self.window?.rootViewController!.performSegue(withIdentifier: "googleSegue", sender: nil) does nothing when it should be occurring and taking the user to the connected view controller after they sign in. The print statement does occur so there is nothing wrong in that regard.

For reference, this is what happens in the viewController file:

@IBAction func googleSignInTapped(_ sender: Any) {
        print("User has chosen to sign with Google.")
        GIDSignIn.sharedInstance().signIn()
    }
KoolKid
  • 45
  • 5
  • You were provided answers in this thread: https://stackoverflow.com/questions/62354073/how-do-i-switch-to-a-different-view-controller-from-the-app-delegate/62354540#62354540 Is it not working? – rs7 Jun 17 '20 at 20:59
  • I thought I replied but from what I understood, the SceneDelegate is oriented towards iPadOS. How would that solve my issue? Maybe I misinterpreted it. Also since that thread, rather than directly changing the view controller, I am trying to get the segue to go through. – KoolKid Jun 17 '20 at 23:27
  • I tried putting the function in SceneDelegate and it didn't work. – KoolKid Jun 17 '20 at 23:34
  • Understood. What iOS versions do you support? iOS13 and above? – rs7 Jun 18 '20 at 02:25
  • Yes sticking only to iPhone. – KoolKid Jun 18 '20 at 04:38
  • Sorry for the delay. Why don't you have a login screen as your rootViewController. You could initially show an activity indicator until the user is authenticated. Once the user is authenticated, remove the activity indicator and either segue to the mainViewController (if successful) or remain in the Login screen and ask the user to login. – rs7 Jun 20 '20 at 04:06

0 Answers0