根据用户Firebase登录状态显示View Controller

时间:2017-01-25 21:17:45

标签: swift firebase uiviewcontroller swift3

在我的应用程序中,我想显示不同的ViewControllers,具体取决于用户是否已登录。就像用户登录当前VC_A一样,如果没有,则显示VC_B。我在vc_login中尝试了以下代码:

 if let user = FIRAuth.auth()?.currentUser {

        let vc = self.storyboard?.instantiateViewController(withIdentifier: "vc_home") as! ViewController_Home
        self.present(vc, animated: true, completion: nil)

   } else {

     // Do Nothing

   } 

1 个答案:

答案 0 :(得分:1)

你可以这样做(提示在代码注释中):

if FIRAuth.auth()?.currentUser?.uid == nil {
  // user is not logged in
  // present VC_B
} else {
  // user is logged in
  // present VC_A
}

或者你可以像这样使用三元条件运算符:

FIRAuth.auth()?.currentUser?.uid == nil ? presentViewControllerB() : presentViewControllerA()

func presentViewControllerA() {
  // call if logged in
}

func presentViewControllerB() {
  // call if not logged in
}