gpt4 book ai didi

ios - 数据未显示在 firebase 数据库中

转载 作者:行者123 更新时间:2023-11-30 11:49:42 25 4
gpt4 key购买 nike

我尝试通过创建帐户 View Controller 添加用户数据,其中包含所有 UITextFields(密码、确认密码、名字、姓氏、电话号码)。当点击创建帐户按钮时,用户的电子邮件会显示在 Firebase 网站的身份验证部分中,但名字、姓氏和电话号码文本字段中的用户信息不会传递到数据库中。我是 iOS 开发新手,从未使用过 firebase,所以我不确定问题是什么。应用程序运行时不会崩溃。

下面是我的创建帐户 View Controller

提前致谢

import UIKit
import FirebaseAuth
import QuartzCore
import FirebaseDatabase
import Firebase

class CreateAccount: UIViewController {

var refUsers: DatabaseReference!

@IBOutlet weak var emailTextField: UITextField!
@IBOutlet weak var passwordTextField: UITextField!
@IBOutlet weak var confirmPasswordTextField: UITextField!
@IBOutlet weak var firstNameTextField: UITextField!
@IBOutlet weak var lastNameTextField: UITextField!
@IBOutlet weak var phoneNumberTextField: UITextField!

@IBOutlet weak var alreadyHaveAccountLabel: UILabel!

@IBAction func loginButtonTapped(_ sender: Any) {

performSegue(withIdentifier: "showLoginScreen", sender: self)
}


override func viewDidLoad() {
super.viewDidLoad()
self.refUsers = Database.database().reference().child("Users");
// Do any additional setup after loading the view.
}


override func viewDidAppear(_ animated: Bool) {


if Auth.auth().currentUser != nil {

print("success")

self.presentMainScreen()
}
}


@IBAction func createAccountTapped(_ sender: Any) {

if let email = emailTextField.text, let password = passwordTextField.text {

Auth.auth().createUser(withEmail: email, password: password, completion:{ user, error in
if let firebaseError = error {
print(firebaseError.localizedDescription)
return
} else {


self.addUser()

print("this is the first name:", self.firstNameTextField.text!)
print("this is the last name:", self.lastNameTextField.text!)
print("this is the phone number" , self.phoneNumberTextField.text!)
print("success")
self.presentMainScreen()
}




})

}
}

func addUser(){
let key = refUsers.childByAutoId().key
let user = ["id":key,
"FirstName":firstNameTextField.text! as String,
"LastName":lastNameTextField.text! as String,
"PhoneNumber":phoneNumberTextField.text! as String
]

refUsers.child(key).setValue(user)
}


override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}


/*
// MARK: - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
// Get the new view controller using segue.destinationViewController.
// Pass the selected object to the new view controller.
}
*/


func presentMainScreen(){
let mainstoryboard = UIStoryboard(name: "Main", bundle: nil)
let mainTabController = mainstoryboard.instantiateViewController(withIdentifier: "MainTabController") as! MainTabController
mainTabController.selectedViewController = mainTabController.viewControllers?[0]
self.present(mainTabController, animated: true, completion: nil)
//let storyboard:UIStoryboard = UIStoryboard(name:"Main", bundle:nil)
//let loggedInVC:LoggedInVC = storyboard.instantiateViewController(withIdentifier: "LoggedInVC") as! LoggedInVC
//self.present(loggedInVC, animated: true, completion: nil)
}



}

最佳答案

试试这个:使用更新值代替设置值

let childUpdates = ["/user/\(key)": user]
refUser.updateChildValues(childUpdates)

希望这有帮助:)

关于ios - 数据未显示在 firebase 数据库中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48470083/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com