1

I am using Firebase and GoogleSignIn to sign up user's to my iOS App. I have written all the code, as instructed, in the AppDelegate.swift file. After the login is successful, I want to take the user to another ViewController. My current code is;

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

    print("User signed into Google")
    guard let authentication = user.authentication else { return }
    let credential = FIRGoogleAuthProvider.credential(withIDToken: authentication.idToken,
                                                   accessToken: authentication.accessToken)

    FIRAuth.auth()?.signIn(with: credential, completion: { (user, error) in
        print("User Signed In to Firebase")

        self.databaseRef = FIRDatabase.database().reference()

        self.databaseRef.child("Google User Profiles").child(user!.uid).observeSingleEvent(of: .value, with: { (snapshot) in

            let snapshot = snapshot.value as? NSDictionary

            if(snapshot == nil)
            {
                self.databaseRef.child("Google User Profiles").child(user!.uid).child("name").setValue(user?.displayName)
                self.databaseRef.child("Google User Profiles").child(user!.uid).child("email").setValue(user?.email)
            }


        })

    })
}
Utkarsh Sharma
  • 61
  • 2
  • 12

1 Answers1

1

First, go in your storyboard and set a storyboard ID to your viewController. Storyboard ID Next, go in your viewController and add this methods in your class.

import UIKit

class ViewController2: UIViewController {

    // Change the name of the storyboard if this is not "Main"
    // identifier is the Storyboard ID that you put juste before
    class func instantiate() -> ViewController2 {
        let storyboard = UIStoryboard(name: "Main", bundle: nil)
        let viewController = storyboard.instantiateViewController(withIdentifier: "\(ViewController2.self)") as! ViewController2

        return viewController
    }

}

Then, in your app delegate, when you want to show the viewController do

window?.rootViewController = ViewController2.instantiate()