Kaira Support
Kaira Support

Reputation: 15

How to share multiple Images with WhatsApp using in iOS?

This is the code for sharing only one image with WhatsApp. How can I share multiple images?

@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
            }
        }
    }
}

Upvotes: 0

Views: 1828

Answers (2)

atulkhatri
atulkhatri

Reputation: 11343

As per their official documentation, they don't seem to support multiple image sharing via URL Scheme.

You can however use the iOS Share extension using UIActivityViewController or UIDocumentInteractionController with extension .wai so only WhatsApp is shown in the list.

Upvotes: 1

Rocky
Rocky

Reputation: 3245

You can share multiple images to any using UIActivityViewController, code give below:

@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)
}

Upvotes: 3

Related Questions