0

it has been 5 hours and I have no idea why I am getting this error.

Optional("File at URL: file:///private/var/mobile/Containers/Data/PluginKitPlugin/C6000DDA-6EE8-4DB3-A360-6DDE9EDCA730/tmp/trim.AD2C231C-65C6-4562-A332-39907709ECA0.MOV is not reachable.")

I have a photo picker that only shows the videos and I want to upload the video selected to firebase. I have no idea why I am getting this error. I can upload any video without the photo picker and it works normally so the problem is not with firebase, it is from the swift code.

Here is my code:

func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {

if let pickedVideo = info[UIImagePickerController.InfoKey.mediaURL] as? NSURL {

        print("PICKED VIDEO")
        print("FIRST URL")
        print(pickedVideo)


        // try to upload here

        let url = URL(fileURLWithPath: pickedVideo.absoluteString!)

        print("NEW URL")
        print(url)

        let storageReference = Storage.storage().reference().child("upload")



                   // Start the video storage process
        storageReference.putFile(from: url , metadata: nil, completion: { (metadata, error) in
                       if error == nil {
                           print("Successful video upload")
                           storageReference.downloadURL { (url, error) in

                                   // Add a new document with a generated ID
                                   let db = Firestore.firestore()
                                   var ref: DocumentReference? = nil
                                   ref = db.collection("Posts").addDocument(data: [
                                   "caption": "self.CaptionTextField.text!",
                                   "isOnlyForMembers": "self.isOnlyForMembers!",
                                   "url": url!,
                                   "numberOfLikes": 0,
                                   "time": Timestamp(date: Date())
                                   ]) { err in
                                   if let err = err {
                                   print("Error adding document: \(err)")
                                   } else {
                                   print("Document added with ID: \(ref!.documentID)")
                                   db.collection("Posts").document(ref!.documentID).setData(["ID": ref!.documentID], merge: true)
                                  // self.activityIndi.stopAnimating()
                               //    self.performSegue(withIdentifier: "topostedsuccess", sender: self)


                                       }
                               }




                           }


                       } else {
                           print(error?.localizedDescription)
                       }
                   })




        }



    }
Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
jmapps9
  • 87
  • 1
  • 7
  • URL `absoluteString` it is not the same as the URL `path`. It contains the URL scheme `"file://"`. You need to pass the URL `path` to the fileURLWithPath initializer `URL(fileURLWithPath: pickedVideo.path)` or pass the `absoluteString` to the string initializer `URL(string: pickedVideo.absoluteString!)` https://stackoverflow.com/questions/40642217/uiimagecontentsoffile-returning-nil-despite-file-existing-in-caches-directory – Leo Dabus Mar 05 '20 at 00:57
  • Note that you should cast your dictionary value straight to a URL. `if let mediaURL = info[.mediaURL] as? URL {` – Leo Dabus Mar 05 '20 at 01:13
  • @LeoDabus Updated my code with your suggestions now I receive an error saying: Error Domain=NSCocoaErrorDomain Code=257 "The file “trim.A57079A5-356C-4EFE-9090-DD4D977B30B6.MOV” couldn’t be opened because you don’t have permission to view it." – jmapps9 Mar 05 '20 at 12:01
  • @LeoDabus but I already have setup to ask for permission, also when uploading a normal photo or uploading a video that I just captured in the app it works normally. – jmapps9 Mar 05 '20 at 12:02
  • It is not clear what is your problem. There is no way to mediaURL be unreachable. Edit your question and add your actual code using mediaURL as I showed in my last comment. – Leo Dabus Mar 05 '20 at 15:39
  • @LeoDabus can you help me with: https://stackoverflow.com/questions/60526667/integrate-onesignal-notification-api-firebase-cloud-function-swift-post-requ – jmapps9 Mar 05 '20 at 21:21
  • The var `pickedVideo` is a URL, then later you get it's absolute string and convert it back to a URL `let url = URL(fileURLWithPath: pickedVideo.absoluteString!)`? Also, this could be a sandboxing issue - did you set those settings to allow file access? – Jay Mar 05 '20 at 21:25

0 Answers0