Reputation: 182
Normally I am updating image with url to firebase storage with:
Storage.storage().reference().child("profile_images").child(fileName).putData(uploadData, metadata: nil) {
[weak self] (metadata, err) in
guard let strongSelf = self else {
return
}
if let err = err {
print(err)
}
guard let profileImageUrl = metadata?.downloadURL()?.absoluteString else {
return
}
ProfilePhotoHandler.Instance.addPhotoUrl(withUrl: profileImageUrl)
}
But right now Xcode 9.3
show warning:
downloadURL()' is deprecated: Use `StorageReference.downloadURLWithCompletion()
Firebase docs still shows old way. Does anyone can help me how to handle it now?
Upvotes: 5
Views: 4971
Reputation: 548
I had the same problem, but I fixed it with this code:
uploadTask.observe(.success) { snapshot in
guard let imageURL = snapshot.metadata?.storageReference?.downloadURL(completion: { (url, error) in if error != nil {
print(error as Any)
} else {
}
}) else { return }
let imageStr = String(describing: imageURL)
DBService.manager.updatePhoto(profileImageUrl: imageStr)
AuthService.manager.updatePhoto(urlString: imageStr)
}
}
Upvotes: 0
Reputation:
With the changes in swift 5.0, metadata no longer has a method of downloadURL.
Instead you must do something along the lines of:
let storageRef = Storage.storage().reference().child("message_images").child(fileName)
storageRef.putData(uploadData, metadata: nil) { (metadata, err) in
if let err = err {
print(err)
}
storageRef.downloadURL(completion: { (url, error) in
if error != nil {
print("Failed to download url:", error!)
return
} else {
//Do something with url
}
})
})
}
Hope this helps. You can also take a look at: this answer, this answer, this answer or just look at the documentation here.
Upvotes: 5