I have been using the code below to show a UIActivityViewController
which worked fine when I was using Xcode 6, Swift 1.2 and iOS 8. However when I updated it shows the UIActivityViewController
but it is completely blank without any of the sharing options. Do you have any suggestions?
if UIDevice.currentDevice().userInterfaceIdiom == .Pad {
let textToShare = textViewOne.text
let objectsToShare = [textToShare]
let activityVC = UIActivityViewController(activityItems: objectsToShare, applicationActivities: nil)
let nav = UINavigationController(rootViewController: activityVC)
nav.modalPresentationStyle = UIModalPresentationStyle.Popover
let popover = nav.popoverPresentationController as UIPopoverPresentationController!
popover.sourceView = self.view
popover.sourceRect = sender.frame
self.presentViewController(nav, animated: true, completion: nil)
} else {
let textToShare = textViewOne.text
let objectsToShare = [textToShare]
let activityVC = UIActivityViewController(activityItems: objectsToShare, applicationActivities: nil)
self.presentViewController(activityVC, animated: true, completion: nil)
}
On iPad, you must present the view controller in a popover. Therefore, on iPad, you must configure `popoverPresentationController` by providing either a sourceView and sourceRect or a barButtonItem.
A view controller that you use to offer standard services from your app.
The title that appears at the top of the the UIActivityItemController is intially simply, "/", with no subtitle, and then after about a second it changes to, "System@snap-2237692", with a subtitle of "File", as per the screenshot below.
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.
Swift 4.0
let shareText = "Hi"
let activity = UIActivityViewController(activityItems: shareText, applicationActivities: nil)
activity.excludedActivityTypes = []
if UIDevice.current.userInterfaceIdiom == .pad {
activity.popoverPresentationController?.sourceView = self.view
activity.popoverPresentationController?.sourceRect = sender.frame
}
self.present(activity, animated: true, completion: nil)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With