0

I'm working on an iOS app that allows users to save the captured video on their iOS devices. After some research, now I'm able to save the video on the device, but I was wondering what kind of situation the app fail to save the video. In the completion block of performChages, it takes two parameters, saved and error in the code below. Since the first parameter is a type of Bool, I think there are some cases when fails to save the video on the device. But it also has an error, so I was wondering what situation could return an error.

success

true if Photos successfully applied the changes requested in the block; otherwise, false.

error

If an error occurs, an NSError object describing the error; otherwise, nil.

Another question is how can I handle when I get success = false in the completion handler. Is it possible to try save the video once more when I get success as false?

guard let writer: AVAssetWriter = recorder.writer else { return }
PHPhotoLibrary.shared().performChanges({() -> Void in
    PHAssetChangeRequest.creationRequestForAssetFromVideo(atFileURL: writer.outputURL)
}, completionHandler: {saved, error -> Void in
    if let error = error {
        print("error: \(error)")
    } else if saved {
        print("saved")
    } else {
        print("failed")
    }
})
Yuuu
  • 715
  • 1
  • 9
  • 32
  • The error could be anything -- file is corrupted, phone is out of storage, etc etc. If you print out `error.localizedDescription` it will tell you what happened. As far as handling it, it's up to you. You can retry, you can pop up an alert to the user that it failed, add a little fail icon next to the video, the list goes on... – johnny May 26 '22 at 20:10
  • @johnny Ahh, I get that... I'll use .localizedDescription then!! Thank you so much!! – Yuuu May 26 '22 at 23:41

0 Answers0