试图将某些类型的用户路由到不同的主屏幕Swift

时间:2017-08-10 12:32:36

标签: ios swift authentication if-statement firebase

我正在使用两种不同类型的用户创建应用程序。我希望一种类型的用户转到一个主屏幕,另一种类型的用户转到不同的主屏幕。这是基于名为“accountType”的属性完成的,该属性在创建用户时默认设置为“reporter”,并且可以在稍后由管理员更改。我无法编写if语句来实现这一点:

//create the user in Firebase
    if let email = emailAddressTextField.text, let pass = passwordTextField.text{
        Auth.auth().createUser(withEmail: email, password: pass, completion: { (user, error) in
            if let u = user{
                //user is found, go to home screen
                var lastName = self.lastNameTextField.text
                var firstName = self.firstNameTextField.text
                var accountType = "reporter"
                self.ref.child("Users").child((user?.uid)!).setValue(["email": email, "first_name": firstName, "last_name": lastName, "accountType": accountType])

                let userID = Auth.auth().currentUser?.uid

//尝试将正确类型的用户提供给正确的主屏幕

                if self.ref.child("Users").child((userID?.uid)!).child("accountType") = "reporter" {
                    self.performSegue(withIdentifier: "goToHome1", sender: self)
                }
                else {
                    self.performSegue(withIdentifier: "goToHome2", sender: self)
                    }

            }

1 个答案:

答案 0 :(得分:1)

您正在做的只是定义Firebase路径,您还必须在引用该路径时观察数据库中的值,试试这个: -

 self.ref.child("Users").child((userID?.uid)!).child("accountType").observeSingleEvent(of: .value, with: {(Snapshot) in

        if let accountT = Snapshot.value as? String{

            // You have retrieved the account type
            // Now Segue

            if accountT == "reporter"{

                self.performSegue(withIdentifier: "goToHome1", sender: self)

            }else{

                self.performSegue(withIdentifier: "goToHome2", sender: self)

            }

        }else{

            // There was no node found in the database
            // named 'accountType'

        }


    }, withCancel: {(Error) in

        // There was some error with the firebase call
        // Handle that error

        print(Error.localizedDescription)


    })