UIActivityViewController on iPad

I was struggling with the above suggestion with SWIFT 5 since:

activity.popoverPresentationController?.sourceRect = sender.frame

does NOT WORK in some cases, since sender is not available in scope. Try instead to set with a CGRECT, something like:

activityController.popoverPresentationController?.sourceRect = CGRect(x: self.view.bounds.midX, y: self.view.bounds.midY,width: 0,height: 0)

I hope this helps some people.


This has fixed it.

        let objectsToShare = [textToShare]
        let activityVC = UIActivityViewController(activityItems: objectsToShare, applicationActivities: nil)
        activityVC.title = "Share One"
        activityVC.excludedActivityTypes = []

        activityVC.popoverPresentationController?.sourceView = self.view
        activityVC.popoverPresentationController?.sourceRect = sender.frame

        self.presentViewController(activityVC, animated: true, completion: nil)

in swift 3.0:

let objectsToShare = [textToShare]
let activityVC = UIActivityViewController(activityItems: objectsToShare, applicationActivities: nil)
activityVC.title = "Share One"
activityVC.excludedActivityTypes = []

activityVC.popoverPresentationController?.sourceView = self.view
activityVC.popoverPresentationController?.sourceRect = sender.frame

self.present(activityVC, animated: true, completion: nil)