【问题标题】:Perform Segue from App Delegate - Swift从 App Delegate 执行 Segue - Swift
【发布时间】:2018-12-05 02:34:37
【问题描述】:

我正在使用 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)
            }


        })

    })
}

【问题讨论】:

  • 您在哪个视图控制器中有 google 登录按钮?
  • LoginViewController
  • 该链接上没有任何内容提及segues。
  • 使用 NSNotificationCenter 将数据从 appdelegate 传递到 loginview 控制器。然后从 loginviewcontroller 执行 segue 到另一个 viewcontroller

标签: swift xcode firebase google-signin


【解决方案1】:

首先,进入您的情节提要并为您的 viewController 设置情节提要 ID。 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()

【讨论】:

  • 这里,我们想把用户带到哪个 ViewController 上,我们当前在哪个 ViewController 上?
  • ViewController2是我们要带用户的viewController。
猜你喜欢
  • 2015-01-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-05-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多