2

I have looked through several SO questions (e.g. this one, or this one) to find a way how to share image directly to WhatsApp. They all seem to follow the instructions from official documentation and so I ended up having this method:

extension WhatsAppInviter {
    func shareImageViaWhatsapp(image: UIImage, onView: UIView) {
        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) {
                    guard let imageData = image.pngData() else { debugPrint("Cannot convert image to data!")
                        return }

                    let tempFile = URL(fileURLWithPath: NSHomeDirectory()).appendingPathComponent("Documents/image.wai")
                    do {
                        try imageData.write(to: tempFile, options: .atomic)
                        documentInteractionController = UIDocumentInteractionController(url: tempFile)
                        documentInteractionController?.uti = "net.whatsapp.image"
                        documentInteractionController?.presentOpenInMenu(from: CGRect.zero, in: onView, animated: true)

                    } catch {
                        UIAlertController.presentAlertWith(title: "Error", message: "There was an error while processing.", actions: [UIAlertAction(title: "OK", style: .default, handler: nil)])
                        return
                    }

                } else {
                    UIAlertController.presentAlertWith(title: "Error", message: "Cannot open Whatsapp, make sure Whatsapp is installed on your device.", actions: [UIAlertAction(title: "OK", style: .default, handler: nil)])
                }
            }
        }
    }
}

The official documentation explicitly states:

Alternatively, if you want to show only WhatsApp in the application list (instead of WhatsApp plus any other public/*-conforming apps) you can specify a file of one of aforementioned types saved with the extension that is exclusive to WhatsApp:

images - «.wai» which is of type net.whatsapp.image

videos - «.wam» which is of type net.whatsapp.movie

audio files - «.waa» which is of type net.whatsapp.audio

When triggered, WhatsApp will immediately present the user with the contact/group picker screen. The media will be automatically sent to a selected contact/group.

However, when executing this code, I keep getting the document interaction controller as you can see here:

Options menu

Am I doing something wrong or did something change with iOS 13? How can I skip this menu, or at least show only WhatsApp there?

Community
  • 1
  • 1
Milan Nosáľ
  • 19,169
  • 4
  • 55
  • 90

0 Answers0