首页 > 解决方案 > 如何在 Swift 中创建类似 Android 上的共享选项

问题描述

我需要展示一个选项来分享一些细节,比如在 Android 上使用 Swift。如何在屏幕上显示要共享的应用程序列表?请看下面的截图:

在此处输入图像描述

标签: iosswiftshare

解决方案


尝试这样的事情:

private func shareWhatYouWant() {

    //pop invite view
    var textToShare = "Your message"
    if let myWebsite = NSURL(string: "Yourwebsite.com") {
        let objectsToShare = [textToShare, myWebsite] as [Any]
        let activityVC = UIActivityViewController(activityItems: objectsToShare, applicationActivities: nil)
        //New Excluded Activities Code

        activityVC.completionWithItemsHandler = {
            activity, sucess, items, error in
            if !sucess {
                print("cancelled")
                return
            }
            if activity == UIActivityType.postToFacebook {
                print("share on facebook")
            }
            if activity == UIActivityType.postToTwitter {
                print("share on twitter")
            }
            if activity == UIActivityType.message {
                print("share on message")
            }
            if activity == UIActivityType.addToReadingList {
                print("air drop select")
            }
        }

        activityVC.popoverPresentationController?.sourceView = sender as? UIView
        self.present(activityVC, animated: true, completion: nil)
    }
}

推荐阅读