Value of type 'StorageMetadata' has no member 'downloadURL'

Solution 1:

Can you try

// Create a reference to the file you want to download
let starsRef = storageRef.child("images/stars.jpg")

// Fetch the download URL
starsRef.downloadURL { url, error in
  if let error = error {
    // Handle any errors
  } else {
    // Get the download URL for 'images/stars.jpg'
  }
}

Solution 2:

This is my version for Swift 3 / Swift 4.

Explanation of what happens in the code.

This is essentially the same answer as Sh_Khan's. But in his example the User already knows the bucket path. In my example, we get the path from an upload task. This was what has lead me to this question as well as what I think op was looking for as he was looking for metadata.downloadURL() replacement.

class StorageManagager {


    private let storageReference: StorageReference

    init() {

        // first we create a reference to our storage
        // replace the URL with your firebase URL
        self.storageReference = Storage.storage().reference(forURL: "gs://MYAPP.appspot.com")
    }

    // MARK: - UPLOAD DATA
    open func uploadData(_ data: Data, named filename: String, completion: @escaping (URL? , Error?) -> Void) {

        let reference = self.storageReference.child(filename)
        let metadata = StorageMetadata()
        metadata.contentType = "ourType" // in my example this was "PDF"

        // we create an upload task using our reference and upload the 
        // data using the metadata object
        let uploadTask = reference.putData(data, metadata: metadata) { metadata, error in

            // first we check if the error is nil
            if let error = error {

                completion(nil, error)
                return
            }

            // then we check if the metadata and path exists
            // if the error was nil, we expect the metadata and path to exist
            // therefore if not, we return an error
            guard let metadata = metadata, let path = metadata.path else {
                completion(nil, NSError(domain: "core", code: 0, userInfo: [NSLocalizedDescriptionKey: "Unexpected error. Path is nil."]))
                return
            }

            // now we get the download url using the path
            // and the basic reference object (without child paths)
            self.getDownloadURL(from: path, completion: completion)
        }

        // further we are able to use the uploadTask for example to 
        // to get the progress
    }

    // MARK: - GET DOWNLOAD URL
    private func getDownloadURL(from path: String, completion: @escaping (URL?, Error?) -> Void) {

        self.storageReference.child(path).downloadURL(completion: completion)
    }

}