如何在iOS中使用WhatsApp共享多个图像?

时间:2018-01-19 06:21:01

标签: ios swift whatsapp

这是与WhatsApp只共享一个图像的代码。我如何分享多个图像?

@IBAction func whatsappShareWithImages(_ sender: AnyObject)
{
    let urlWhats = "whatsapp://app"
    if let urlString = urlWhats.addingPercentEncoding(withAllowedCharacters:CharacterSet.urlQueryAllowed) {
        if let whatsappURL = URL(string: urlString) {
            if UIApplication.shared.canOpenURL(whatsappURL as URL) {
                if let image = UIImage(named: "whatsappIcon") {
                    if let imageData = UIImageJPEGRepresentation(image, 1.0) {
                        let tempFile = URL(fileURLWithPath: NSHomeDirectory()).appendingPathComponent("Documents/whatsAppTmp.wai")
                        do {
                            try imageData.write(to: tempFile, options: .atomic)
                            self.documentInteractionController = UIDocumentInteractionController(url: tempFile)
                            self.documentInteractionController.uti = "net.whatsapp.image"
                            self.documentInteractionController.presentOpenInMenu(from: CGRect.zero, in: self.view, animated: true)
                        } catch {
                            print(error)
                        }
                    }
                }
            } else {
                 UIAlertView(title: "Error",  message : "Please install the WhatsApp application", delegate:nil, cancelButtonTitle:"Ok").show()
                // Cannot open whatsapp
            }
        }
    }
}

2 个答案:

答案 0 :(得分:2)

您可以使用UIActivityViewController分享多个图片,代码如下:

@IBAction func share(_ sender: Any) {

    let image1 = UIImage(named: "a.jpg")
    let image2 = UIImage(named: "b.jpg")
    let image3 = UIImage(named: "c.jpg")

    let dataToShare = [image1, image2, image3]

    let activityController = UIActivityViewController(activityItems: dataToShare, applicationActivities: nil)
    self.present(activityController, animated: true, completion: nil)
}

答案 1 :(得分:1)

根据他们的official documentation,他们似乎不支持通过URL Scheme进行多个图片共享。

然而,您可以使用UIActivityViewControllerUIDocumentInteractionController使用扩展名为.wai的iOS共享扩展程序,这样只会在列表中显示WhatsApp。