UIActivityViewController on iPad

2019-05-19 00:12发布

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)

    }

2条回答
smile是对你的礼貌
2楼-- · 2019-05-19 00:51

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)
查看更多
The star\"
3楼-- · 2019-05-19 01:01

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)
查看更多
登录 后发表回答