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:

1)
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.

2)
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 {
                        print("cancelled")
                        return
                    }
                    
                    if activityType!.rangeOfString("whatsapp") != nil{
                        print("whatsapp")
                        
                        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.

More Reads

We learn, We share

Top 5 Hacks to Get the Best from your Indian Offshore Team

Getting the best out of an offshore team in India is always a challenging task, inspite of the obvious cost advantages. Timezone differences, communication challenges, project management, quality of the code delivered, are all real issues that clients face. The solution however is surprisingly simple. Having delivered quite a few offshoring projects successfully, we have put … Continue reading "Top 5 Hacks to Get the Best from your Indian Offshore Team"

A 5-Point Guide to Hiring the Right UX/UI Design Firm

Hiring the right UX/UI design team for your Mobile and Web development is never easy. What you see as an outstanding portfolio of work, may not guarantee a great end-product for you. You need to be aware of your product design needs, the constraints and the UX team’s strengths. And spend some time with the … Continue reading "A 5-Point Guide to Hiring the Right UX/UI Design Firm"

Next up home

Contact

Lets build cool stuff

Share your contact information & we will get in touch!

I want (Tell us more about your dream project)