title: Share PHAsset stored in camera roll using UIActivityViewController
body: <p>I'm storing images and videos in a Camera Roll album using PhotoKit, and want to allow the user to share them using UIActivityViewController. If I pass UIActivityViewController a UIImage instance, it works as expected, probably because the image data is passed in memory. However, videos need to be passed by URL because there's no video analogue to UIImage. When I pass a URL to a video, I get an error "Could not create sandbox extension". If I pass a URL to an image, I get a similar error.</p>
  
  <p>Based on <a href="https://stackoverflow.com/questions/28208980/video-not-attached-for-mail-when-sharing-video-using-uiactivityviewcontroller">this</a>, it seems as though I might be able to get around this error by exporting the assets to the Documents directory, and passing UIActivityViewController the URL to the asset in Documents. However, I've read elsewhere that the Camera Roll can serve a similar purpose, and it goes to reason that the Camera Roll would be one of the few places that can hold data for sharing between apps.</p>
  
  <p>Is there a way to pass UIActivityViewController URLs to Camera Roll assets without copying them to Documents? Is there a better way to be sharing images and video that are already in Camera Roll?</p>
  
  <hr>
  
  <p><strong>Implementation Details:</strong></p>
  
  <p>I'm generating URLs for assets using this:</p>
  
  <pre class="lang-swift prettyprint-override"><code>    func videoFor(asset: PHAsset, resultHander: @escaping (AVAsset?, AVAudioMix?, [AnyHashable : Any]?) -&gt; Void) {
          imageManager.requestAVAsset(forVideo: asset, options: nil, resultHandler: resultHander)
      }
  
      func urlFor(asset: PHAsset, resultHandler: @escaping (URL?) -&gt; Void) {
          if ( asset.mediaType == .video ) {
              videoFor(asset: asset) { (asset, audioMix, info) in
                  let asset = asset as! AVURLAsset
                  resultHandler(asset.url)
              }
          }
  
          else if ( asset.mediaType == .image ) {
              let options: PHContentEditingInputRequestOptions = PHContentEditingInputRequestOptions()
              options.canHandleAdjustmentData = {(adjustmeta: PHAdjustmentData) -&gt; Bool in
                  return true
              }
              asset.requestContentEditingInput(with: options, completionHandler: {(contentEditingInput: PHContentEditingInput?, info: [AnyHashable : Any]) -&gt; Void in
                  resultHandler(contentEditingInput!.fullSizeImageURL as URL?)
              })
          }
  
          else {
              resultHandler(nil)
          }
      }
  </code></pre>
  
  <p>Here is the full error I get in console when trying to share an image by URL:</p>
  
  <pre><code>Failed to determine whether URL /var/mobile/Media/DCIM/100APPLE/IMG_0201.JPG (n) is managed by a file provider
  Could not create sandbox extension. Error: Error Domain=NSPOSIXErrorDomain Code=1 "Operation not permitted" UserInfo={NSLocalizedDescription=Could not create sandbox extension of type com.apple.app-sandbox.read for URL /var/mobile/Media/DCIM/100APPLE/IMG_0201.JPG. Error: No such file or directory}
  </code></pre>
  
  <p>... and for a video:</p>
  
  <pre><code>Failed to determine whether URL /var/mobile/Media/DCIM/100APPLE/IMG_0202.M4V (n) is managed by a file provider
  Could not create sandbox extension. Error: Error Domain=NSPOSIXErrorDomain Code=1 "Operation not permitted" UserInfo={NSLocalizedDescription=Could not create sandbox extension of type com.apple.app-sandbox.read for URL /var/mobile/Media/DCIM/100APPLE/IMG_0202.M4V. Error: Operation not permitted}
  </code></pre>