ios – How to upload a url to firebase storage


I have tried uploading a video to firebase but I am running into a error trying to upload my post

Error uploading video: File at URL:
file:///Users/cliffhanger/Library/Developer/CoreSimulator/Devices/1B893FD3-5DEA-4B68-8B9E-8ACBF599B295/data/Containers/Data/Application/7E6A710D-7EA7-40A2-97A4-89BCD450C81E/tmp/.com.apple.Foundation.NSItemProvider.ewy5Py/B79FC0AC-BA09-493B-8FA2-739B9FF546BF-A879605C-C06F-409F-9592-1ADD80F1F437.mov
is not reachable. Ensure file URL is not a directory, symbolic link,
or invalid url.

I need help rewriting my code to upload my user post to firebase, How do I properly upload the selected video to remove this error

The Full Create View Code


struct UploadMediaView: View {
    @StateObject private var shareVideo = ShareVideo()
      
      var body: some View {
          VStack {
              Button(action: {
                  shareVideo.selectVideo()
              }) {
                  Text("Select Video")
              }
              
              Button("Upload Video") {
                  shareVideo.uploadVideo()
              }
          }
      }
  }

The ShareVideo


import SwiftUI
import PhotosUI
import FirebaseStorage

class ShareVideo: NSObject, ObservableObject {
    @Published var videoURL: URL?
    
    func selectVideo() {
        PHPhotoLibrary.requestAuthorization { status in
            switch status {
            case .authorized:
                print("Access granted.")
            case .denied, .restricted:
                print("Access denied.")
            case .notDetermined:
                print("Authorization not determined.")
            @unknown default:
                fatalError("New authorization status is available.")
            }
        }
        
        var configuration = PHPickerConfiguration()
        configuration.filter = .videos
        let picker = PHPickerViewController(configuration: configuration)
        picker.delegate = self
        UIApplication.shared.windows.first?.rootViewController?.present(picker, animated: true)
    }
    
    func uploadVideo() {
        guard let videoURL = videoURL else {
            return
        }
        
        let storage = Storage.storage()
        let storageRef = storage.reference()
        let videoRef = storageRef.child("videos/\(UUID().uuidString).mp4")
        let metadata = StorageMetadata()
        metadata.contentType = "video/mp4"
        
        videoRef.putFile(from: videoURL, metadata: metadata) { metadata, error in
            if let error = error {
                print("Error uploading video: \(error.localizedDescription)")
            } else {
                print("Video uploaded successfully.")
            }
        }
    }
}

extension ShareVideo: PHPickerViewControllerDelegate {
    func picker(_ picker: PHPickerViewController, didFinishPicking results: [PHPickerResult]) {
        picker.dismiss(animated: true)
        
        guard let result = results.first else {
            return
        }
        
        result.itemProvider.loadFileRepresentation(forTypeIdentifier: UTType.movie.identifier) { url, error in
            if let error = error {
                print("Error loading video: \(error.localizedDescription)")
            } else if let url = url {
                DispatchQueue.main.async {
                    self.videoURL = url
                }
            }
        }
    }
}

``

Latest articles

spot_imgspot_img

Related articles

Leave a reply

Please enter your comment!
Please enter your name here

spot_imgspot_img