刚刚更新了我的可可豆荚,从而更新了火力库。这行代码是旧方法,现在是错误的:

let downloadURL = metaData!.downloadURL()!.absoluteString
let values: Dictionary<String, Any> = ["download_url": downloadURL]

下面的代码是现在提取URL字符串的正确方法。但是,我需要有关如何将该字符串放入数组以保存到Firebase的帮助。
storageRef.downloadURL(completion: {(url, error) in
    if error != nil {
     print(error!.localizedDescription)
       return
    }
    let downloadURL = url?.absoluteString

})

let values: Dictionary<String, Any> = ["download_url": downloadURL]

如何将“值”保存为子
let databaseRef = Database.database().reference()
let path = databaseRef.child("posts").child((self.loggedInUser?.uid)!).childByAutoId()
path.setValue(values) { (error, ref) -> Void in
    if error != nil {
        print("error saving post in db")
    } else {
        let storageRef = Storage.storage().reference().child("posts_requests").child((self.loggedInUser?.uid)!).child(snapshot.childSnapshot(forPath: "uid").value as! String).child(snapshot.childSnapshot(forPath: "uid").value as! String).child(snapshot.childSnapshot(forPath: "imageID").value as! String)
        storageRef.delete(completion: { error in
            if let error = error {
                print(error)
            } else {
                print("Successful Delete")
            }
        })

    }
}

使用以下答案...

当我使用下面提交的答案时,我得到一个打印输出,说“用户无权访问gs://shoppeer-e7270.appspot.com/(null)”。我要做的就是抓住该URL字符串并将其添加到我的“值”(即字典)中。

我用于图像上传以及保存为子的完整代码
        let photosRef = storage.reference().child("posts").child((loggedInUser?.uid)!)
    let usersRef = Database.database().reference().child("Businesses")
    let databaseRef = Database.database().reference()
    let imageName = NSUUID().uuidString
    let photoRef = photosRef.child("\(uid)")
    let postID = databaseRef.child("posts").child((loggedInUser?.uid)!).childByAutoId().key
    var downloadURLSting = String()

    photoRef.child("\(imageName)").putData(data!, metadata: nil) { (metaData,error) in
        if let error = error {
            print("there was an error")
            print(error.localizedDescription)
            return
        } else {
            // store downloadURL
            storage.reference().downloadURL(completion: {(url, error) in
                if error != nil {
                    print(error!.localizedDescription)
                    return
                }
                let downloadURL = url?.absoluteString

                let values: Dictionary<String, Any> = ["uid": uid, "caption": caption ?? "", "download_url": downloadURL, "timestamp": ServerValue.timestamp(), "businessName":loggedInUserData?["businessName"] as! String, "businessStreet":loggedInUserData?["businessStreet"] as! String, "businessCity":loggedInUserData?["businessCity"] as! String, "businessState":loggedInUserData?["businessState"] as! String, "businessZIP":loggedInUserData?["businessZIP"] as! String, "businessPhone":loggedInUserData?["businessPhone"] as! String, "businessWebsite":loggedInUserData?["businessWebsite"] as! String, "businessLatitude":loggedInUserData?["businessLatitude"] as! String, "businessLongitude":loggedInUserData?["businessLongitude"] as! String, "facebookURL":loggedInUserData?["facebookURL"] as! String, "twitterURL":loggedInUserData?["twitterURL"] as! String, "instagramURL":loggedInUserData?["instagramURL"] as! String, "googleURL":loggedInUserData?["googleURL"] as! String, "yelpURL":loggedInUserData?["yelpURL"] as! String, "foursquareURL":loggedInUserData?["foursquareURL"] as! String, "snapchatURL":loggedInUserData?["snapchatURL"] as! String, "imageID": imageName, "postID": postID]

                // store downloadURL at database
                let databaseRef = Database.database().reference()
                let path = databaseRef.child("posts").child((loggedInUser?.uid)!).childByAutoId()
                path.setValue(values) { (error, ref) -> Void in
                    if error != nil {
                        print("error saving post in db")
                    } else {
                        // reset caption field
                        self.descriptionTextView.text = ""
                        // reset placeholder image
                        self.imageView.image = UIImage(named: "filterPlaceholder")
                        MBProgressHUD.hide(for: self.view, animated: true)
                        let viewConrolller = self.storyboard?.instantiateViewController(withIdentifier: "Business Profile") as! UITabBarController
                        self.present(viewConrolller, animated: true, completion: nil)
                    }
                }
            })
        }
    }

可以直接下载网址,字符串为nil
 let photosRef = storage.reference().child("posts").child((loggedInUser?.uid)!)
    let usersRef = Database.database().reference().child("Businesses")
    let databaseRef = Database.database().reference()
    let imageName = NSUUID().uuidString
    let photoRef = photosRef.child("\(uid)")
    let postID = databaseRef.child("posts").child((loggedInUser?.uid)!).childByAutoId().key
    photoRef.child("\(imageName)").putData(data!, metadata: nil) { (metaData,error) in
        if let error = error {
            print("there was an error")
            print(error.localizedDescription)
            return
        } else {
            // store downloadURL
            photoRef.downloadURL(completion: {(url, error) in
                if error != nil {

                    guard let downloadURL = url?.absoluteString else { return }

                    let values: Dictionary<String, Any> = ["uid": uid, "caption": caption ?? "", "download_url": downloadURL, "timestamp": ServerValue.timestamp(), "businessName":loggedInUserData?["businessName"] as! String, "businessStreet":loggedInUserData?["businessStreet"] as! String, "businessCity":loggedInUserData?["businessCity"] as! String, "businessState":loggedInUserData?["businessState"] as! String, "businessZIP":loggedInUserData?["businessZIP"] as! String, "businessPhone":loggedInUserData?["businessPhone"] as! String, "businessWebsite":loggedInUserData?["businessWebsite"] as! String, "businessLatitude":loggedInUserData?["businessLatitude"] as! String, "businessLongitude":loggedInUserData?["businessLongitude"] as! String, "facebookURL":loggedInUserData?["facebookURL"] as! String, "twitterURL":loggedInUserData?["twitterURL"] as! String, "instagramURL":loggedInUserData?["instagramURL"] as! String, "googleURL":loggedInUserData?["googleURL"] as! String, "yelpURL":loggedInUserData?["yelpURL"] as! String, "foursquareURL":loggedInUserData?["foursquareURL"] as! String, "snapchatURL":loggedInUserData?["snapchatURL"] as! String, "imageID": imageName, "postID": postID]

                    // store downloadURL at database
                    let databaseRef = Database.database().reference()
                    let path = databaseRef.child("posts").child((loggedInUser?.uid)!).childByAutoId()
                    path.setValue(values) { (error, ref) -> Void in
                        if error != nil {
                            print("error saving post in db")
                        } else {
                            // reset caption field
                            self.descriptionTextView.text = ""
                            // reset placeholder image
                            self.imageView.image = UIImage(named: "filterPlaceholder")
                            MBProgressHUD.hide(for: self.view, animated: true)
                            let viewConrolller = self.storyboard?.instantiateViewController(withIdentifier: "Business Profile") as! UITabBarController
                            self.present(viewConrolller, animated: true, completion: nil)
                        }
                    }
                } else {
                    print(error!.localizedDescription)
                    print("error")
                    return
                }
            })
        }
    }

最佳答案

valuesDictionary,而不是Array,但是如果要向其中添加downloadURL,则需要在storageRef.downloadURL(completion:)的完成处理程序中执行此操作,因为这是异步方法。

storageRef.downloadURL(completion: {(url, error) in
    if error != nil {
     print(error!.localizedDescription)
       return
    }
    let downloadURL = url?.absoluteString
    let values: Dictionary<String, Any> = ["download_url": downloadURL]
})

关于ios - Swift Firebase metaData!.downloadURL()!. absoluteString,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/52611994/

10-12 00:20