Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UIActivityViewController on iPad

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)

    }
like image 604
Tom Coomer Avatar asked Oct 01 '15 14:10

Tom Coomer


People also ask

How do I present Uiactivityviewcontroller on iPad?

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.

What is Uiactivityviewcontroller?

A view controller that you use to offer standard services from your app.

How is the title of a Uiactivityviewcontroller set?

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.


2 Answers

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.

like image 184
Donovan Marsh Avatar answered Sep 26 '22 01:09

Donovan Marsh


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)
like image 43
Aayushi Avatar answered Sep 26 '22 01:09

Aayushi