UIActivityViewController on iPad

2019-05-19 00:36发布

问题:

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)

    }

回答1:

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)


回答2:

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)