Search code examples
swiftpermissionsswift4uiimagepickercontroller

Requesting permission to use a video file that is picked with UIImagePickerController after app is killed


So I am currently picking a video from a gallery, using UIImagePickerController. I have this code:

 func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
        if let videoURL = info[UIImagePickerController.InfoKey.mediaURL] as? URL {
            MediaManager.shared.sourceType = picker.sourceType
            self.metadata.videoURL = videoURL
            ...
        }
        else if let asset =  info[UIImagePickerController.InfoKey.phAsset] as? PHAsset {
            asset.getURL(completionHandler: { url in
                self.metadata.videoURL = url
               ...
            })
        }
        else if let ref =  info[UIImagePickerController.InfoKey.phAsset] as? URL {
            self.metadata.videoURL = ref
           ...
        }
        else {
           ...
        }


    }

So, I save video url and based on that url I am able to get video data etc.

The thing is, next time I restart the app, I want to be able to get that video again, without using picker. But it seems I have no permissions for this. Is there any way to ask an iOS for a permission that will give me the ability to open video by its url even if I closed the app (or if it crashed)?

EDIT:

so for example, I get something like this as an url:

file:///private/var/mobile/Containers/Data/Application/AC5AD743-924F-430D-9C34-F3848E4CEE75/tmp/4CBAD829-8764-4234-BCCF-BFA571845D55.MOV

but I can't really use it after app is killed. So how to pick something with picker, and reuse it later.


Solution

  • Apple documentation says:

    Important: Although they are safe to use while your app is running, file reference URLs are not safe to store and reuse between launches of your app because a file’s ID may change if the system is rebooted. If you want to store the location of a file persistently between launches of your app, create a bookmark as described in Locating Files Using Bookmarks.

    So I will suggest use bookmarkData(options:includingResourceValuesForKeys:relativeTo:) method to create bookmark for existing URL.

        func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey: Any]) {
            guard let mediaType = info[.mediaType] as? String,
                mediaType == kUTTypeMovie as String,
                let url = info[.mediaURL] as? URL else {
                return
            }
    
            do {
                let bookmarkData = try url.bookmarkData()
                UserDefaults.standard.set(bookmarkData, forKey: "mediaURL")
            } catch {
                print("bookmarkData error: \(error)")
            }
        }
    
        func mediaURL() -> URL? {
            guard let urlData = UserDefaults.standard.data(forKey: "mediaURL") else {
                return nil
            }
    
            var staleData = false
            let mediaURL = try? URL(resolvingBookmarkData: urlData, options: .withoutUI, relativeTo: nil, bookmarkDataIsStale: &staleData)
    
            return staleData ? nil : mediaURL
        }