Reputation: 625
I need help with this coding, i'm not sure what has happened.please don't duplicate this question or similar as I already tried any solution on the internet and nothing worked. I am trying to get "access to download URL after upload".
I attempted :I use the autocomplete of the metadata it shows no url option the closest I got was metadata.path or metadata.storageReference.
2nd attempt: I know The API had changed at version 5.0.0, so I use StorageReference's downloadURL method. that gives me an error too maybe I am putting it on the wrong line of code or else.
here is the class :-
import UIKit
import Firebase
import FirebaseDatabase
import FirebaseStorage
import SwiftKeychainWrapper
class SignUpVC: UIViewController, UIImagePickerControllerDelegate,UINavigationControllerDelegate{
@IBOutlet weak var userImagePicker: UIImageView!
@IBOutlet weak var userNameField: UITextField!
@IBOutlet weak var SignUpBtn: UIButton!
var userId: String!
var emailField: String!
var passwordField: String!
var imagePicker: UIImagePickerController!
var imageselected = false
var userName: String!
//Do any Additional SetUp after loading the view
override func viewDidLoad() {
super .viewDidLoad()
imagePicker = UIImagePickerController()
imagePicker.delegate = self
imagePicker.allowsEditing = true
}
override func viewDidAppear(_ animated: Bool) {
if let _ = KeychainWrapper.standard.string(forKey: "uid") {
performSegue(withIdentifier: "ToMessges", sender: nil)
}
}
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
if let image = info[UIImagePickerController.InfoKey.editedImage] as? UIImage{
userImagePicker.image = image
imageselected = true
}else{
print("Image wasent selected!")
imagePicker.dismiss(animated: true, completion: nil)
}
}
func setuser(img: String){
let userData = [
"username": userName!,"userimage": img]
KeychainWrapper.standard.set(userId, forKey: "uid")
let location = Database.database().reference().child("users").child(userId)
location.setValue(userData)
dismiss(animated: true, completion: nil)
}
func uploadImg (){
if userNameField.text == nil {
SignUpBtn.isEnabled = false
}else {
userName = userNameField.text
SignUpBtn.isEnabled = true
}
guard let img = userImagePicker.image, imageselected == true else {
print("Image needs to be selected")
return
}
if let imgData = img.jpegData(compressionQuality: 0.2) {
let imgUid = NSUUID().uuidString
let metadata = StorageMetadata()
metadata.contentType = "image/Jpeg"
Storage.storage().reference().child(imgUid).putData(imgData, metadata:metadata){(metaData,error
) in
if error != nil {
print("Did not upload Image!")
}else {
print("Uploaded")
let downloadURL = metadata.downloadURL()?.absoulteString //error here
if let url = downloadURL {
self.setUser(img:url)
}
}
}
}
}
Upvotes: 0
Views: 653
Reputation: 3271
As per Firebase doc, you may there is no direct property downloadURL
in StorageMetadata
class. So you may have to use dictionaryRepresentation
and access download link using mediaLink key.
Example:
if let downloadURL = metadata.dictionaryRepresentation()["mediaLink"] as? String{
self.setUser(img:downloadURL)
}
Upvotes: 2