从App Delegate执行Segue-Swift

时间:2018-06-26 11:25:03

标签: swift xcode firebase google-signin

我正在使用Firebase和GoogleSignIn将用户的帐户注册到我的iOS应用。按照指示,我已在AppDelegate.swift文件中编写了所有代码。登录成功后,我想将用户带到另一个ViewController。我当前的代码是;

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)
            }


        })

    })
}

1 个答案:

答案 0 :(得分:1)

首先,进入情节提要,并将情节提要ID设置为viewController。 Storyboard ID 接下来,进入您的viewController并将此方法添加到您的类中。

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
    }

}

然后,在您的应用程序委托中,当您想要显示viewController时

window?.rootViewController = ViewController2.instantiate()