使用Firebase更新用户信息

时间:2018-07-10 01:13:29

标签: swift xcode firebase firebase-realtime-database uiimageview

我正试图允许用户编辑其个人资料信息。我可以看到用户,名称和电话号码已更新。电子邮件和个人资料图片不是。我希望允许用户单独更改其信息。如果我尝试更新单个字段,则其余字段将被清空。如果有人可以帮助我解决我的问题,将不胜感激。用户在注册时选择个人资料图片,并将其存储在Firebase中。我只是无法弄清楚如何允许用户更改单个字段。

    @objc func handleUpdate() {
    if ((updateemailTextField.text != "" || nameTextField.text != "") && (updateemailTextField.text != nil || nameTextField.text != nil)){

        let userRef = Database.database().reference().child("users").child(Auth.auth().currentUser!.uid)
        if let new_Email = updateemailTextField.text{

            Auth.auth().currentUser!.updateEmail(to: updateemailTextField.text!) { error in

                if error == nil{
                    userRef.updateChildValues(["email" : new_Email ], withCompletionBlock: {(errEM, referenceEM)   in

                        if errEM == nil{
                            print(referenceEM)
                        }else{
                            print(errEM?.localizedDescription)
                        }
                    })
                }
            }
        }

        if let new_Name = nameTextField.text as? String{

            userRef.updateChildValues(["name" : new_Name ], withCompletionBlock: {(errNM, referenceNM)   in

                if errNM == nil{
                    print(referenceNM)
                }else{
                    print(errNM?.localizedDescription)
                }
            })
        }

    }else{

    }

    if ((updateemailTextField.text != "" || phonenumberTextField.text != "") && (updateemailTextField.text != nil || phonenumberTextField.text != nil)){

        let userRef = Database.database().reference().child("users").child(Auth.auth().currentUser!.uid)
        if let email = updateemailTextField.text{

            Auth.auth().currentUser!.updateEmail(to: updateemailTextField.text!) { error in

                if error == nil{
                    userRef.updateChildValues(["email" : email ], withCompletionBlock: {(errEM, referenceEM)   in

                        if errEM == nil{
                            print(referenceEM)
                        }else{
                            print(errEM?.localizedDescription)
                        }
                    })
                }
            }
            do {
            }
        }

        if let newphonenumber = phonenumberTextField.text{

            userRef.updateChildValues(["phonenumber" : newphonenumber ], withCompletionBlock: {(errNM, referenceNM)   in

                if errNM == nil{
                    print(referenceNM)
                }else{
                    print(errNM?.localizedDescription)
                }
            })
        }

    }else{

    }
}
}

0 个答案:

没有答案