我已经看过一些关于在应用程序中注册或注册后将用户信息保存到DataBase的教程,但问题是他通过那里保存了所有用户,我想要的是用户名来保存用户你认为是什么专业或更好的方式,或聊天应用程序
并不重要@IBAction func SignUp(_ sender: Any) {
if username.text != nil, email.text != nil, password.text != nil, fullname.text != nil {
activityIndicator.startAnimating()
Auth.auth().createUser(withEmail: email.text!, password: password.text!, completion: { (user, error) in
if error != nil {
AlertController.showAlert(self, titel: "Error", message:".Fill all fields\n .User do exists\n .Network error ")
self.activityIndicator.stopAnimating()
return
}
let uid = user?.uid
let storageRef = Storage.storage().reference(forURL: "gs://,,,,,,,,,.com").child("profile_image").child(uid!)
if let profileImg = self.selectedImage, let imageData = UIImageJPEGRepresentation(profileImg, 0.1){
storageRef.putData( imageData, metadata: nil, completion: { (metadata, error) in
if error != nil{
return
}
let profileImageUrl = metadata?.downloadURL()?.absoluteString
let ref = Database.database().reference()
let usersReferance = ref.child("users")
let newUserReferance = usersReferance.child(uid!)
newUserReferance.setValue(["fullname": self.fullname.text, "username": self.username.text, "email": self.email.text, "password": self.password.text, "profileImageUrl": profileImageUrl])
})
}
self.dismiss(animated: true, completion: nil)
self.activityIndicator.stopAnimating()
})
}
}
}
答案 0 :(得分:1)
如果您想按用户名保存用户,则可以按照以下步骤进行操作
@IBAction func SignUp(_ sender: Any) {
if username.text != "", email.text != "", password.text != "", fullname.text != "" {
activityIndicator.startAnimating()
Auth.auth().createUser(withEmail: email.text!, password: password.text!, completion: { (user, error) in
if error != nil {
AlertController.showAlert(self, titel: "Error", message:".Fill all fields\n .User do exists\n .Network error ")
self.activityIndicator.stopAnimating()
return
}
let uid = user?.uid // here you are providing userID
let storageRef = Storage.storage().reference(forURL: "gs://.........com").child("profile_image").child(uid!)
if let profileImg = self.selectedImage, let imageData = UIImageJPEGRepresentation(profileImg, 0.1){
storageRef.putData( imageData, metadata: nil, completion: { (metadata, error) in
if error != nil{
return
}
let profileImageUrl = metadata?.downloadURL()?.absoluteString
let ref = Database.database().reference()
let usersReferance = ref.child("users")
// let newUserReferance = usersReferance.child(uid!) //here in ref you are creating a node with UID
//use your username
let newUserReferance = usersReferance.child(username.text!)
//set child values here as you doing
newUserReferance.setValue(["fullname": self.fullname.text, "username": self.username.text, "email": self.email.text, "password": self.password.text, "profileImageUrl": profileImageUrl])
})
}
self.dismiss(animated: true, completion: nil)
self.activityIndicator.stopAnimating()
})
}
}
2。 1)我不认为这将是一个更好的方法,就像你用用户名设置节点如果signUp其他用户填充相同的用户名数据将被新数据替换
2)如果您使用一些用户名有效的检查,它会起作用,但只是为了担心需要在数据库中另外引用它以检查用户名是否可用
- 在整个数据库中检查是否采用了用户名
- 或创建一个包含用户名列表的节点,然后获取所有数组并使用文本字段验证进行离线检查以获得快速响应,或者甚至可以进行观察事件