Home > front end >  Novice question, how can I return a downloadURL from firebase.Storage in swift?
Novice question, how can I return a downloadURL from firebase.Storage in swift?

Time:06-28

I'm working in iOS (swiftUI) to be precise. I'm uploading images to firebase-storage where I have retrieved a downloadURL after each image has uploaded however I've become stuck with getting my urlStr (the fetched downloadURL) returned out. Your help is greatly appreciated.

Code segment that uploads and then retrieves the downloadURL:

        if let data = data {
        mainRef.putData(data, metadata: metadata) { (metadata, error) in
            if let error = error {
                print("Error while uploading file: ", error)
            }
            if let metadata = metadata {
                let pathRef = self.storage.reference(withPath: "\(dir)/\(metadata.name ?? "Image not found")")
                pathRef.downloadURL { url, error in
                    if let error = error {
                        print(error.localizedDescription)
                            return
                    } else {
                        // Get the download URL
                        let urlStr:String = (url?.absoluteString) ?? ""
                        print(urlStr)  // <--------- Pass back to newSite function.
                    }
                }
            }
        }
    }
    return(urlReturn)

initial call to the above:

currentSite.imageMain = storageManager.upload(image: image, siteRef: newSiteRef.documentID, size: 1000, dir: "ImagesMain/")

CodePudding user response:

you could try something like this approach using a completion handler:

func upload(image: ....., completion: @escaping (String) -> Void) ) { // <-- here
    // .....
    if let data = data {
    mainRef.putData(data, metadata: metadata) { (metadata, error) in
        if let error = error {
            print("Error while uploading file: ", error)
        }
        if let metadata = metadata {
            let pathRef = self.storage.reference(withPath: "\(dir)/\(metadata.name ?? "Image not found")")
            pathRef.downloadURL { url, error in
                if let error = error {
                    print(error.localizedDescription)
                    completion("")  // <-- here todo deal with errors
                        return
                } else {
                    // Get the download URL
                    let urlStr: String = (url?.absoluteString) ?? ""
                    print(urlStr)  
                    completion(urlStr)  // <-- here pass back urlStr
                }
            }
        }
    }
   }
   return(urlReturn)
}

Use it like this:

        currentSite.imageMain = storageManager.upload(image: image, siteRef: newSiteRef.documentID, 
size: 1000, dir: "ImagesMain/") { urlString in // <-- here
            print("---> urlString: \(urlString)" ) // <-- here
        }

Since you don't show a complete working example code, you will have to adjust the approach to your needs.

  • Related