0

I am trying to get a video from a user's library, and for the application to be able to play them back that video whenever they want. Currently I am saving it as a bookmark but this gets wiped on device restart. Currently using didSelect(url: URL?) from the videoPickerDelegate to receive the url/video from the user if that makes a difference. Is there a more preferred way to make a copy so the video does not get lost on restart?

Another avenue I am trying is writing the bookmark to a file in documentDirectory/movieDirectory to try to load later, and currently just working through the wrinkles of that. I am not uploading any of the users data/videos to any servers so must all be local

I've also seen some information on security scoped bookmarks but was unsure if that's the correct avenue.

Thanks for any guidance!

DannyG
  • 177
  • 8
  • check this link https://stackoverflow.com/questions/29482738/swift-save-video-from-nsurl-to-user-camera-roll – Chandaboy Jul 13 '23 at 05:07

1 Answers1

0

Perhaps it was a path issue, but I'll post the final solution that worked for me for anyone else that is looking to save a video and have it persist despite a device reset.

Using this link as a guide: https://www.tutorialspoint.com/how-to-download-a-video-using-a-url-and-save-it-in-an-photo-album-using-swift

Save the Video to file (assuming you already have the URL):

        let urlData = NSData(contentsOf: video)
        let galleryPath = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)[0];
        let filePath="\(galleryPath)/[YOUR_VIDE_NAME].MOV"
        urlData!.write(toFile: filePath, atomically: true)

I was then able to access the video using:

let galleryPath = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)[0];
let filePath="\(galleryPath)/[YOUR_VIDE_NAME].MOV"
                                    
var testUrl = URL(fileURLWithPath: filePath)
player = AVPlayer(url: testUrl);
DannyG
  • 177
  • 8