Denis Windover
Denis Windover

Reputation: 445

Share image via WhatsApp

I have in my app button to share image via whatsapp and it does work. But there is some strange thing appears in the menu of UIDocumentInteractionController on some devices. This is the code:

        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 imageData = UIImageJPEGRepresentation(self.ivFramedPicture.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.delegate = self
                        self.documentInteractionController.uti = "net.whatsapp.image"
                        self.documentInteractionController.presentOpenInMenu(from: CGRect.zero, in: self.view, animated: true)

                    } catch {
                        print(error)
                    }
                }

            } else {
                // Cannot open whatsapp
            }
        }
    }

enter image description here If I click on the 1 whatsapp icon it sends some file that doesn't open on iPhones (Android opens that file like image)

enter image description here

Does anyone can help to resolve that problem? I want only one icon with share image, that's it. Thanks

Upvotes: 2

Views: 1449

Answers (2)

PGDev
PGDev

Reputation: 24341

Simply use UIActivityController for sharing functionality instead of all that code.

Example:

    if let image = self.ivFramedPicture.image
    {
        let activityViewController = UIActivityViewController(activityItems: [image], applicationActivities: nil)
        self.present(activityViewController, animated: true, completion: nil)
    }

Upvotes: 3

chronikum
chronikum

Reputation: 736

Maybe try using the UIActivityViewController

Upvotes: 0

Related Questions