Getting a sourceRect for UIActivityViewController from a UIDocumentBrowserViewController

300 Views Asked by At

I am trying to add a custom activity to my UIDocumentBrowserController long-tap menu - a UIActivityViewController. The default share activity, when selected, is pointing nicely to the icon of the file that the user just long-tapped.

How can I get the sourceRect so that my activity could be presented in a similar way?

let shareBundle = UIDocumentBrowserAction(identifier:
           "com.mycompany.myapp.shareBundle", localizedTitle: "Share with Data",
                availability: [.menu], handler: { urls in
            if urls.count > 0 {
                let objectsToShare: [URL] = [urls]
                let activityVC = UIActivityViewController(activityItems: objectsToShare, applicationActivities: nil)
                activityVC.popoverPresentationController?.sourceView = self.view
                activityVC.popoverPresentationController?.sourceRect = // That's the problem!
                self.present(activityVC, animated: true, completion: nil)
            }
        })
1

There are 1 best solutions below

0
On

Looks like it is currently not possible to get the view of the selected document.

For now I ended up just positioning the ActivityView at the center of the screen and disabling the arrows:

 if let popOverController = activityVC.popoverPresentationController {
      popOverController.sourceView = self.view
      popOverController.sourceRect = CGRect(x: self.view.bounds.midX, y: self.view.bounds.midY, width: 0, height: 0)
      popOverController.permittedArrowDirections = []
 }