我无法在应用中创建新帐户。创建帐户按钮没有任何作用

时间:2019-05-16 15:52:16

标签: swift firebase firebase-authentication

我正在尝试为我的应用程序完成注册过程,但是当我点击创建帐户时,它什么也没做。它只是保留在同一页面上。创建帐户按钮应触发以下方法:

@IBAction func createAccountPress(_ sender: Any){

        guard usernameField.text != "", emailField_SU.text != "", passwordField_SU.text != "", confirmPasswordField_SU.text != "" else {return}

        if passwordField_SU == confirmPasswordField_SU{
            if let email = emailField_SU.text, let password = passwordField_SU.text{
                Auth.auth().createUser(withEmail: email, password: password)
                { (user,error) in
                    if let error = error{
                        print(error.localizedDescription)
                    }
                    if let user = user {
                        let changeRequest = Auth.auth().currentUser!.createProfileChangeRequest()
                        changeRequest.displayName = self.usernameField.text
                        changeRequest.commitChanges(completion:nil)
                        let imageRef = self.userStorage.child("\(user.user.uid).jpg")
                        let data = self.userImgView.image!.jpegData(compressionQuality:0.5)

                        let uploadTask = imageRef.putData(data!, metadata: nil, completion: {(metadata,error) in
                            if error != nil{
                                print(error?.localizedDescription)
                            }
                            imageRef.downloadURL(completion: {(url, error) in
                                if error != nil {
                                    print(error!.localizedDescription)
                                }
                                if let url = url {
                                    let userInfo: [String:Any] = ["uid": user.user.uid,
                                                                  "username": self.usernameField.text!,
                                                                  "urlToImage":url.absoluteString]

                                    self.ref.child("users").child(user.user.uid).setValue(userInfo)
                                    let vc = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier:"signinVC")
                                    self.present(vc, animated:true, completion:nil)
                                }
                            })
                    })
                    uploadTask.resume()
                        //self.storeUserData(userId: (user.user.uid))
                        //KeychainWrapper.standard.set((user.user.uid), forKey:"uid")
                    self.performSegue(withIdentifier:"backToSignin", sender: nil)
                }
            }
        } else {
            print("Password does not match")
        }
    }
}

我完全没有抛出任何错误,并且该应用程序也没有退出。它只是什么也没做,至少在应用程序应该选择回到登录视图时。您发现我的代码有什么问题吗?

1 个答案:

答案 0 :(得分:0)

那里有一些冗余代码,但我认为这可能是问题的一部分

if passwordField_SU == confirmPasswordField_SU

该代码正在验证密码 field 是否与确认密码 field 相同,因为它们是两个不同的字段,所以它们不是相同的。

您可能正在尝试确认用户两次输入相同的密码,并且密码不为空

if passwordField_SU.text == confirmPasswordField_SU.text {