简体   繁体   English

如何在与iOS Swift中的UIActivityViewController共享的WhatsApp中设置不同的参数?

[英]How to set different parameters in WhatsApp sharing with UIActivityViewController in iOS Swift?

I am using UIActivityViewController for sharing contents from app. 我正在使用UIActivityViewController从应用程序共享内容。

I want to share different content with different sharing apps. 我想与不同的共享应用程序共享不同的内容。

Like in Message output will be like this. 就像在Message输出中一样。 image + text + URL 图片+文字+ URL

in Whatsapp i would like to share like below image text + URL 在Whatsapp中,我想像下面的图片文本+ URL一样共享

How can i do this? 我怎样才能做到这一点? See below screen shots for this. 参见下面的屏幕截图。

It took me a good time figuring this out, but that's how it worked for me: 我花了很长时间才弄清楚这一点,但这就是它的工作原理:

Think of this problem in two steps: first, we need to tell to the UIActivityViewController which content we want to share. 分两步思考这个问题:首先,我们需要告诉UIActivityViewController我们要共享哪些内容。 Second, we need to return the content based on each social media, either a link, an image or a text. 其次,我们需要根据每个社交媒体(链接,图像或文本)返回内容。 Its up to the social media app to tell which content it can handle, and it will only show up if we share the right kind of content. 它取决于社交媒体应用程序,以告知它可以处理哪些内容,并且只有在我们共享正确类型的内容时才会显示。

In the first step, we will try to fool the social media apps, saying that we want to share an UIImage and a NSObject. 在第一步中,我们将尝试愚弄社交媒体应用程序,说我们要共享UIImage和NSObject。 This will open most of the social media apps to share. 这将打开大多数社交媒体应用程序以进行共享。

In the second step, we will identify which social media app the user has clicked, and return the appropriated content for it. 在第二步中,我们将确定用户单击了哪个社交媒体应用程序,并为其返回适当的内容。

Implementation: 执行:

create two UIActivityItemSource, one which will return an UIImage and other which will return the NSObject. 创建两个UIActivityItemSource,一个将返回UIImage,另一个将返回NSObject。

class SocialActivityItem: NSObject, UIActivityItemSource {
    var img: UIImage?
    var url: URL?

    convenience init(img: UIImage, url: URL) {
        self.init()
        self.img = img
        self.url = url
    }

    // This will be called BEFORE showing the user the apps to share (first step)
    func activityViewControllerPlaceholderItem(_ activityViewController: UIActivityViewController) -> Any {
        return img!
    }

    // This will be called AFTER the user has selected an app to share (second step)
    func activityViewController(_ activityViewController: UIActivityViewController, itemForActivityType activityType: UIActivityType?) -> Any? {
        //Instagram
        if activityType?.rawValue == "com.burbn.instagram.shareextension" {
            return img!
        } else {
            return url
        }
    }
}

and

class TextActivityItem: NSObject, UIActivityItemSource {
    var textToShare: String?

    convenience init(textToShare: String) {
        self.init()
        self.textToShare = textToShare
    }

    // This will be called BEFORE showing the user the apps to share (first step)
    func activityViewControllerPlaceholderItem(_ activityViewController: UIActivityViewController) -> Any {
        return NSObject()
    }

    // This will be called AFTER the user has selected an app to share (second step)
    func activityViewController(_ activityViewController: UIActivityViewController, itemForActivityType activityType: UIActivityType?) -> Any? {
      var text = ""
      if activityType?.rawValue == "net.whatsapp.WhatsApp.ShareExtension" {
          text = "Sharing on Whatsapp"
      }

      if activityType == UIActivityType.postToFacebook {
          text = "Sharing on Facebook"
      }
      return text
    }
}

Then, you just need to set everything up: 然后,您只需要设置所有内容:

let url = URL(string: "www.google.com")!
let socialProvider = SocialActivityItem(img: img, url: url)
let textProvider = TextActivityItem(textToShare: "Sharing on social media!")
let activityViewController = UIActivityViewController(activityItems: [socialProvider, textProvider], applicationActivities: nil)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM