iosswiftios14phasset

phResourceManager.writeData(for... wont run on iOS 14


Before I updated to iOS 14 on my iPhone, this code was working perfectly. After, iOS 14 this is weirdly not running... it is very odd and I have not seen any solution online, additionally from my investigation, I have not been able to see any change.

This code is used in order to retrieve a videoURL for this video from the imported Camera Roll (I use import Photos...).

phResourceManager.writeData(for: resource.last!, toFile: newURL!, options: resourceRequestOptions) { (error) in
    if error != nil {
        print(error, "not c67omplted error?")
    } else {
        print("woah completedd 345?")
        
        newUserTakenVideo.videoURL = newURL
        print(newUserTakenVideo.videoURL, "<--?")
    }
}

EDIT: To be clear, it "does not run" means the compleition block never runs... as in it never even runs and gives an error, the compleition block simply never is called (nothing prints at least..)

And here is a print statement printing out all the values I pass in to the parameters:

phResourceManager: <PHAssetResourceManager: 0x282d352c0>

resource.last:

Optional(<PHAssetResource: 0x28128bc00> {
    type: video
    uti: public.mpeg-4
    filename: v07044090000bu6n1nhlp4leque7r720.mp4
    asset: C97B45D3-7039-4626-BA3E-BCA67912A2A9/L0/001
    locallyAvailable: YES
    fileURL: file:///var/mobile/Media/DCIM/113APPLE/IMG_3404.MP4
    width: 576
    height: 1024
    fileSize: 4664955
    analysisType: unavailable
    cplResourceType: Original
    isCurrent: YES
}) 

newURL: Optional(file:///var/mobile/Containers/Data/Application/E2792F47-142E-4601-8D5B-F549D03C9AFE/Documents/Untitled%2027228354.MP4)

resourceRequestOptions: <PHAssetResourceRequestOptions: 0x28230d480>

Note: this is the decleration for the resource variable:

let resource = PHAssetResource.assetResources(for: (cell?.assetPH)!)

Solution

  • I have a solution to this! Swift 4+, tested on iOS 14!

    I looked through using a PHAssetResourceRequest, but the file names were messed with in the process, and it generally didn't work with my sandbox. Then I also tried requesting a AVPlayerItem from the PHAsset but this too, did not work with sandboxing...

    But then, I tried simply using PHAssetResourceManager.default().writeData(... and seemingly started working!

    I tested a bit more and seemed to work, here is the full code:

                        let resource = PHAssetResource.assetResources(for: (cell?.assetPH)!)
                        let resourceRequestOptions = PHAssetResourceRequestOptions() 
                        
                        let newURL = ExistingMediaVC.newFileUrl
                        
                        PHAssetResourceManager.default().writeData(for: resource.last!, toFile: newURL!, options: resourceRequestOptions) { (error) in
                            if error != nil {
                                print(error, "error")
                            } else {
                                print("good")
                                newUserTakenVideo.videoURL = newURL
                            }
                        }
    

    It is quite simple!! Tell me if anything is not working, and note I still use the ExisitingMedia.fileURL variable you used in your original code as well :)