繁体   English   中英

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

[英]How to share multiple Images with WhatsApp using in iOS?

这是与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
            }
        }
    }
}

您可以使用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)
}

根据他们的官方文档 ,他们似乎不支持通过URL方案共享多个图像。

但是,您可以通过扩展名为.wai UIActivityViewControllerUIDocumentInteractionController使用iOS共享扩展名,因此列表中仅显示WhatsApp。

暂无
暂无

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

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