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

11,240

Solution 1

You've probably updated your Firebase pods. In Firebase 5.0 they got rid of the metaData?.downladURL() function. You have to follow the updated docs on their website. Copying from there:

// Data in memory
let data = Data()

// Create a reference to the file you want to upload
let riversRef = storageRef.child("images/rivers.jpg")

// Upload the file to the path "images/rivers.jpg"
let uploadTask = riversRef.putData(data, metadata: nil) { (metadata, error) in
  guard let metadata = metadata else {
    // Uh-oh, an error occurred!
    return
  }
  // Metadata contains file metadata such as size, content-type.
  let size = metadata.size
  // You can also access to download URL after upload.
  riversRef.downloadURL { (url, error) in
    guard let downloadURL = url else {
      // Uh-oh, an error occurred!
      return
    }
  }
}

Firebase Storage: https://firebase.google.com/docs/storage/ios/upload-files

Solution 2

Update your pods and put this instead:

func uploadProfileImage(_ image:UIImage, completion: @escaping ((_ url:URL?)->())) {
    guard let uid = Auth.auth().currentUser?.uid else { return }
    let storageRef = Storage.storage().reference().child("user/\(uid)")

    guard let imageData = UIImageJPEGRepresentation(image, 0.75) else { return }

    let metaData = StorageMetadata()
    metaData.contentType = "image/jpg"

    storageRef.putData(imageData, metadata: metaData) { metaData, error in
        if error == nil, metaData != nil {

            storageRef.downloadURL { url, error in
                completion(url)
                // success!
            }
            } else {
                // failed
                completion(nil)
            }
        }
    }

Solution 3

    // MARK: - handleRegister
func handleRegister() {
    guard let email = emailTextField.text, let password = passwordTextField.text, let name = nameTextField.text else {
        print("Error")
        return
    }

    Auth.auth().createUser(withEmail: email, password: password) { (user, error) in
        if error != nil {
            // print("Error")
            return
        }

        // MARK: - hadleLogin// 

        guard let uid = Auth.auth().currentUser?.uid  else {
            return
        }

        // MARK: - image successful authenficated user

        let imageName = NSUUID().uuidString
        let storageRef = Storage.storage().reference().child("profile_images").child("\(imageName).png")

        if let profileImageUrl = self.profileImageView.image, let  uploadData = UIImageJPEGRepresentation(self.profileImageView.image!, 0.1) {
            storageRef.putData(uploadData, metadata: nil, completion: { (metadata, error) in

                if error != nil, metadata != nil {
                    print(error ?? "")
                    return

                }

                storageRef.downloadURL(completion: { (url, error) in
                    if error != nil {
                        print(error!.localizedDescription)
                        return
                    }
                    if let profileImageUrl = url?.absoluteString {
                        let values = ["name": name, "email": email, "profileImageUrl": profileImageUrl]
                        self.registeUserIntoDatabaseWithUID(uid: uid, values: values as [String : AnyObject])
                    }
                })
            })
        }
    }
}

Solution 4

Delete the FirebaseStorage from your pod Folder.
Close Xcode.
Go to podFile , Change the pod 'Firebase/Storage' to pod Firebase/Storage','~>4.0'
Save the podFile and open terminal
write : pod update and Hit enter
Open Xcode and write the following :

if let profileImg     = self.selectedImage, let photoData = profileImg.jpegData(compressionQuality:0.1) {
                storageRef.putData(photoData, metadata: nil, completion: { (metadata, error) in
                    if error != nil  {
                        return
                    }

                   let profileImageUrl = metadata?.downloadURL()?.absoluteString
                    let ref                 = Database.database().reference()
                    let usersReference      = ref.child("user")

                    let newUserReference    = usersReference.child(uid!)
                    newUserReference.setValue(["username" :self.usernameTextField.text!, "email" :self.emailTextField.text!, "ProfileImageUrl":profileImageUrl])
                })

Run the project, the errors would be gone

Share:
11,240
Admin
Author by

Admin

Updated on June 08, 2022

Comments

  • Admin
    Admin about 2 years

    Hey I need help with this coding, i'm not sure what has happened. It was working the other day. The error comes up after if let ur; = metaData?.downladURL()

    func uploadProfileImage(_ image:UIImage, completion: @escaping ((_ url:URL?)->())) {
            guard let uid = Auth.auth().currentUser?.uid else { return }
            let storageRef = Storage.storage().reference().child("user/\(uid)")
    
            guard let imageData = UIImageJPEGRepresentation(image, 0.75) else { return }
    
            let metaData = StorageMetadata()
            metaData.contentType = "image/jpg"
    
            storageRef.putData(imageData, metadata: metaData) { metaData, error in
                if error == nil, metaData != nil {
                    if let url = metaData?.downloadURL() {
                        completion(url)
                    } else {
                        completion(nil)
                    }
                    // success!
                } else {
                    // failed
                    completion(nil)
                }
            }
        }
    
  • Failed Scientist
    Failed Scientist about 6 years
    Kindly add some description as well
  • Jeremy Caney
    Jeremy Caney about 4 years
    This may be a correct answer, but it’d be useful to provide additional explanation of your code so developers can understand your reasoning. This is especially useful for new developers who may not be familiar with the syntax. Would you mind updating your comment with additional details?
  • Sunkas
    Sunkas about 4 years
    It does not seem like a good solution to downgrade a framework to use it
  • Sunkas
    Sunkas about 4 years
    Is it just me or why do I need to download the image to get the public URL? What is the file is really large? It will take double the time then.
  • Sunkas
    Sunkas about 4 years
    Is it just me or why do I need to download the image to get the public URL? What is the file is really large? It will take double the time then.
  • Sunkas
    Sunkas about 4 years
    Is it just me or why do I need to download the image to get the public URL? What is the file is really large? It will take double the time then.
  • Sunkas
    Sunkas about 4 years
    Does not compile. downloadURL is a async function, not a property.
  • Charlie
    Charlie about 4 years
    what would be future issues if you downgrade frameworks?
  • keshav
    keshav over 2 years
    i am getting error storage rer has no member put data