-1

I'm working with filemanager. So far I'm able to initialize my directory but when I try to save data there, the error i get is "error: Cannot create file".There seems to be an issue with the way I'm creating my path because when I use a temporary url, I am able to save to the photos library.

Here's the working temporary url code

var tempURL: URL? {
let directory = NSTemporaryDirectory() as NSString
if directory != "" {
  let path = directory.appendingPathComponent("video.mov")
  return URL(fileURLWithPath: path)
}
return nil

}

Here's the code where I execute the recording


  do {
      let videoPath = "SavedVideos"
      var videoDirectory = try FileManager.default.url(for: .documentDirectory, in: .userDomainMask, appropriateFor: nil, create: true)
      videoDirectory = videoDirectory.appending(path: videoPath)
      videoDirectory = videoDirectory.appending(path: "000001")
      videoDirectory = videoDirectory.appending(path: "video.mov")
      movieOutput.startRecording(to: videoDirectory, recordingDelegate: self)
  }
  catch {
      print("error recording to video directory: \(error)")
  }

Again, changing startRecording(to:videoDirectory to startRecording(to: tempURL! , I able to record. Any insights on what I'm doing wrong?

Shehata Gamal
  • 98,760
  • 8
  • 65
  • 87
codechef
  • 55
  • 1
  • 8

1 Answers1

1

The problem is that you add directories to the path (SavedVideos and 000001) without creating them , so Insert createDirectory like below

do {
    var videoDirectory = try FileManager.default.url(for: .documentDirectory, in: .userDomainMask, appropriateFor: nil, create: true)
    videoDirectory = videoDirectory.appending(path: "SavedVideos/000001")
    try FileManager.default.createDirectory(atPath: videoDirectory.path, withIntermediateDirectories: true, attributes: nil)
    videoDirectory = videoDirectory.appending(path: "video.mov")
    movieOutput.startRecording(to: videoDirectory, recordingDelegate: self)
} catch {
    print(error.localizedDescription)
}
Shehata Gamal
  • 98,760
  • 8
  • 65
  • 87
  • This seems like a step in the right direction. I'm no longer getting the error. However, when the recording stops, my didFinishrecordingTo method never triggers so the file isnt being saved for some reason. It does trigger with the temporary url. – codechef Sep 20 '22 at 14:15
  • Can you print error inside `didFinishRecordingTo:from:error` ? – Shehata Gamal Sep 20 '22 at 14:19
  • I realize what I did wrong. Thanks for your help @Sh_kan – codechef Sep 20 '22 at 18:28