Home / IOS Development / How do I upload a photo to Firebase? The code I used worked, but now it stopped working. : iOSProgramming

How do I upload a photo to Firebase? The code I used worked, but now it stopped working. : iOSProgramming



Using SwiftUI, I try to upload an image to Firebase Storage, but it does not upload

So the problem is that my upload image I used for some reason stopped working. It no longer uploads the image to Firebase Storage. When I go to create a post and upload an image, I can see the image on the simulator device, but if I try a new device (or just go to Firebase Storage), I can see that the image I uploaded was actually never uploaded .

The weirdest part about it is that the “url” (string) I have that I have read from actually contains a full string that works. When I upload, I give the string I want to read from it. The reason I know it works is that before (I never changed my Upload Image Code), it actually uploaded the image. On another device, the posts with which the image was uploaded are displayed, but the rest are not.

This is probably very confusing, but below is my Upload Image Code.

import SwiftUI
import Firebase

func UploadImage(imageData: Data,path: String,completion: @escaping (String) -> ()){
    
    let storage = Storage.storage().reference()
    let uid = Auth.auth().currentUser!.uid
    
    storage.child(path).child(uid).putData(imageData, metadata: nil) { (_, err) in
        
        if err != nil{
            completion("")
            return
            
        }
        
        // Downloading Url And Sending Back...
        
        storage.child(path).child(uid).downloadURL { (url, err) in
            if err != nil{
                completion("")
                return
                
            }
            completion("(url!)")
        }
    }
}

Also adds: This is the call I make when the picture is selected:

UploadImage(imageData: self.img_Data, path: "post_Pics") { (url) in
                        
                        ref.collection("Posts").document().setData([
                        
                            "title": self.postTxt,
                            "url": url,
                            "ref": ref.collection("Users").document(self.uid),
                            "time": Date(),
                            
                            "colour": self.colourTxt,
                            "size": self.sizeTxt,
                            "condition": self.conditionTxt,
                            "price": self.priceTxt,
                            "additional" : self.addTxt
                            
                        ]) { (err) in
                            
                            if err != nil{
                                self.isPosting = false
                                return
                            }
                            
                            self.isPosting = false
                            // closing View When Succssfuly Posted...
                            present.wrappedValue.dismiss()
                        }
                    }

Source link