WhatsApp with UIActivityViewController for sharing image

15 February 2016 by Javal Nanda No comments

Hello, iOS buddies!! You might have come across a situation where you want to provide share option to the user in your application to share (Image + Text data).
Your best bet would be to use UIActivityViewController where you have to just pass items to share and appropriate application will be shown in sharing option.
But when it comes to WhatsApp, you can either pass just text or just image but not both. This is due to the limited support from WhatsApp for custom URL scheme on iOS:
Whatsapp FAQ

So, in this case, if you are passing image + text data using UIActivityViewController and if the user selects WhatsApp, only the text data will be sent and not an image.
For sending an image to WhatsApp, we need to use UIDocumentInteractionController.

Workarounds for this:

Detect if a user selected WhatsApp application and use UIDocumentInteractionController to copy an image and send it to WhatsApp. This will open WhatsApp twice, and would not be a pleasant experience to the end user.
But if your sharing requirement includes sharing of data+image and image is the priority this is the best alternative.
Please note in this option, only an image will be sent to WhatsApp and not text.

If Whatsapp is the priority for sharing image data, you can use UIDocumentInteractionController for sharing and not UIActivityViewController.

Please find below the sample code written in swift to detect if user selected WhatsApp for sharing and triggering UIDocumentInteractionController for user to allow WhatsApp from available option:

@IBAction func shareClicked(sender: AnyObject) {

            let objectsToShare = ["[(self.productDetail.code!)] (self.productDetail.name!)", productImg]
                let activityVC = UIActivityViewController(activityItems: objectsToShare, applicationActivities: nil)
                activityVC.completionWithItemsHandler = { activityType, success, items, error in
                    if !success {
                    if activityType!.rangeOfString("whatsapp") != nil{
                        let directoryURL = NSFileManager.defaultManager().URLsForDirectory(.DocumentDirectory, inDomains: .UserDomainMask)[0]
                        let localPath = directoryURL.URLByAppendingPathComponent("whatsAppTmp.wai")
                        UIImageJPEGRepresentation(productImg, 1.0)?.writeToURL(localPath, atomically: true)
                        self.docController = UIDocumentInteractionController(URL: localPath)
                        self.docController.delegate = self
                        self.docController.UTI = "net.whatsapp.image"
                        self.docController.presentOpenInMenuFromRect(CGRectZero, inView: self.view, animated: true)
                        self.docController.presentOpenInMenuFromRect(CGRectZero, inView: self.view, animated: true)
                self.presentViewController(activityVC, animated: true, completion: nil)

Following delegate method is required to be implemented in your controller for providing a view controller for presenting a document preview:

func documentInteractionControllerViewControllerForPreview(controller: UIDocumentInteractionController) -> UIViewController {
        return self

Feel free to contact javal@idyllic-software.com for any queries or connect on linkedin

Javal Nanda

Follow me on Twitter

Subscribe To Our Blog

Get access to proven marketing ideas, latest trends and best practices.

Next up home


Lets build cool stuff

Share your contact information & we will get in touch!

I want (Tell us more about your dream project)