![](/img/trans.png)
[英]How to make faster the loading of the user's profile photo (Firebase, swift)
[英]Swift Firebase - User Registration with Profile Photo
我想弄清楚如何注册用户信息并添加个人资料照片。 问题是 Firebase 文档只提供了使用电子邮件和密码创建用户的方法,它可以工作,但是我该如何注册更多信息?
func createNewUser(name: String, email: String, phone: String, photo: UIImage, password: String, onCompletion: @escaping (Bool, RequestErrors?) -> Void) {
Auth.auth().createUser(withEmail: email, password: password) { (result, error) in
if let error = error {
print("Login error: \(error.localizedDescription)")
onCompletion(false, .authError)
return
}
onCompletion(true, nil)
}
}
我通过参数将信息传递给这个函数,但我不知道如何继续。 有什么帮助吗?
要为用户存储更多信息,您需要使用 Firebase 数据库或 Firestore。 您必须在 pod 文件中添加一个。
例如,以下是在数据库中为用户设置电子邮件的工作方式
var ref: DatabaseReference!
ref = Database.database().reference()
self.ref.child("users").child(user.uid).setValue(["email": user's email])
对于图像存储,您需要使用 Storage,它的工作原理类似。 您还需要更新您的 pod 文件。 您将创建一个引用来存储图像。 所以像 users/profilepics/uid.jpg 这样你就有一个上传任务将图像上传到参考。 有关更多信息,请查看 Firebase 存储文档。
这就是最终的代码。
func createNewUser(name: String, email: String, phone: String, photo: UIImage, password: String, onCompletion: @escaping (Bool, RequestErrors?) -> Void) {
Auth.auth().createUser(withEmail: email, password: password) { (result, error) in
if let error = error {
print("Login error: \(error.localizedDescription)")
onCompletion(false, .authError)
return
}
let imageName = UUID().uuidString
let storageRef = Storage.storage().reference().child("profile_images").child("\(imageName).jpg")
guard let uid = result?.user.uid else { return }
if let uploadData = photo.jpegData(compressionQuality: 0.1) {
storageRef.putData(uploadData, metadata: nil, completion: { (_, error) in
if let error = error {
print(error)
return
}
storageRef.downloadURL(completion: { (url, error) in
if let error = error {
print(error)
return
}
guard let photoUrl = url else { return }
let values = ["displayName": name, "email": email, "phoneNumber": phone, "photoURL": photoUrl.absoluteString]
let ref = Database.database().reference()
let usersReference = ref.child("users").child(uid)
usersReference.updateChildValues(values, withCompletionBlock: { (err, ref) in
if err != nil {
print(err!)
return
}
User.shared = User(uid: uid, displayName: name, email: email, phoneNumber: phone, photoURL: photoUrl)
onCompletion(true, nil)
})
})
})
}
}
当我获得我需要的所有数据时,我会填写我的用户单例。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.