Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sharing Video PHAsset via UIActivityController

I am trying to share video PHAsset via UIActivityController using requestAVAsset. This works with Messaging, but not with AirDrop, indicating as 'Failed'.

PHCachingImageManager.default().requestAVAsset(forVideo: asset, options: nil, resultHandler:
    { (givenAsset, audioMix, info) in

        let videoAsset = givenAsset as! AVURLAsset
        let videoURL = videoAsset.url

        DispatchQueue.main.async {
            let activityViewController = UIActivityViewController(
                activityItems: [videoURL],
                applicationActivities: nil)
            activityViewController.excludedActivityTypes = [UIActivityType.saveToCameraRoll]

            if let popoverPresentationController = activityViewController.popoverPresentationController {
                popoverPresentationController.barButtonItem = (sender)
            }

            self.present(activityViewController, animated: true, completion: nil)
        }
})

This seems to properly put up UIActivityController and only work with certain activities:

  • Messaging - ✔️Works, properly exports video.
  • AirDrop - ✖️Shows "Failed"
  • Dropbox - ✖️Puts up the proper Dropbox View, yet says "Unknown error occurred"

enter image description here

like image 584
Gizmodo Avatar asked Apr 30 '17 15:04

Gizmodo


1 Answers

I've run into similarly odd behavior when working with PHAssets. My guess is this is a (purposely) undocumented security/sandboxing restriction.

I was able to work around this problem by copying the underlying file to a user directory, and then performing the operation on the copied file.

I did this in a loop. Occasionally, the copy fails with a vague file permissions error. When it does, I retry it after a few seconds (using DispatchQueue.main.asyncAfter). Eventually, it works!

like image 185
Anna Dickinson Avatar answered Oct 24 '22 15:10

Anna Dickinson