【问题标题】:How to get the username & photourl from userid - Swift & Firebase如何从用户 ID 获取用户名和照片网址 - Swift & Firebase
【发布时间】:2018-07-30 15:28:34
【问题描述】:

我有另一个 swift 文件,其中存储了来自 Firebase 的所有当前用户信息,但由于某种原因它无法正常工作,看起来好像没有数据。如果有人能帮助我找出我做错了什么,我将不胜感激。

这是我的 Firebase 架构...

class UserService {


    static var currentUserProfile:UserProfile?

    static func observeUserProfile(_ uid: String, completion: @escaping ((_ userProfile: UserProfile?)-> ()) ) {

        let userRef = Database.database().reference().child("users/\(uid)")
        userRef.observe(.value) { (snapshot) in
            var userProfile:UserProfile?

            if let dict = snapshot.value as? [String: Any],
                let username = dict["username"] as? String,
                let photoUrl = dict["photoUrl"] as? String,
                let url = URL(string: photoUrl) {

                userProfile = UserProfile(uid: snapshot.key, username: username, photoUrl: url)

            }
            completion(userProfile)
        }
    }

}

然后我使用用户名和 photoUrl 在我的 setValue 函数中使用它...

func sendDataToDatabaseFound(address: String, breed: String, phone: String, photoUrl: String, timestamp: String) {
        //por the time being photoUrl:String - wasn't added because I can't get the downloadURL
        //         func sendDataToDatabase(address: String, breed: String, phone: String, lostfound: String, photoUrl: String)
        var ref: DatabaseReference!
        ref = Database.database().reference()
        let postsReference = ref.child("posts").child("found")
        let newPostId = postsReference.childByAutoId().key
        let newPostReference = postsReference.child(newPostId)
        // Still need to add the "photo": photoUrl --> newPostReference.setValue(["photoUrl": photoUrl]
        let toId = Auth.auth().currentUser?.uid
        newPostReference.setValue( ["address": address, "breed": breed, "phone": phone,"photoUrl": photoUrl, "timestamp": timestamp, "author": ["userid": toId, "username": UserService.currentUserProfile?.username, "profilePhotoUrl": UserService.currentUserProfile?.photoUrl.absoluteString]]) { (error, ref) in
            if error != nil {
                ProgressHUD.showError(error!.localizedDescription)
                return
            }

【问题讨论】:

    标签: swift firebase firebase-realtime-database author uid


    【解决方案1】:

    如果您查看Auth.auth().currentUser 的代码,您会发现它属于FIRUser 类,在Swift 中被称为UserFIRUserFIRUserInfo 的子类,它具有许多特征,包括 providerID、uid、displayName、photoURL、email 和 phoneNumber。

    您可以像访问uidcurrentUser 一样访问所有这些数据。

    if let user = Auth.auth().currentUser {
        let providerID: String = user.providerID
        let uid: String = user.uid
        let displayName: String = user.displayName
        let photoURL: String = user.photoURL
        let email: String = user.email
    }
    

    【讨论】:

    • 嗨,布赖恩!但是,我怎样才能将该信息发送到 newPostReference 以设置值?另外...我想显示名称是用户名?如果在我的“用户”孩子中,我以用户名的名义拥有它,这仍然有效吗?
    • 嗨 Ava,您可以像在 newPostReference.setValue() 中所做的那样将该数据转换为字典,然后将其作为 .setValue() 块的数据部分上传。
    猜你喜欢
    • 1970-01-01
    • 2017-07-02
    • 2018-11-23
    • 1970-01-01
    • 2015-02-06
    • 2019-05-26
    • 2023-03-16
    • 1970-01-01
    • 2019-03-22
    相关资源
    最近更新 更多