繁体   English   中英

Swift Firebase metaData!.downloadURL()!. absoluteString

[英]Swift Firebase metaData!.downloadURL()!.absoluteString

刚刚更新了我的可可豆荚,因此更新了firebase。 这行代码是旧方法,现在是错误的:

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)
                    }
                }
            })
        }
    }

这个工作只是downloadURL字符串是零

 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:) ,因为那是一个异步方法。

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]
})

下载URL仅完成处理程序中可用。

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
        ...

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM