2

In July 2013 WhatsApp opened their URL schemes for our apps. I have sent text to Whatsapp from my application, but now I would like send a image. How send a image to Whatsapp?

I'm not sure how do it.

Thank you.

Paolpa
  • 319
  • 2
  • 5
  • 10
  • 1
    http://www.whatsapp.com/faq/en/iphone/23559013 If I understand them correctly, just make an instance of UIDocumentInteractionController, init it with your image and then it should be possible to send images via the built-in action button of the UIDocumentInteractionController. – Marc Oct 22 '13 at 18:42
  • Thank you very much. I try it and with UIDocumentInteracionController I got send image via WhatsApp. – Paolpa Oct 24 '13 at 15:22

2 Answers2

6

Per their documentation, you need to use UIDocumentInteractionController. To selectively display only Whatsapp in the document controller (it is presented to the user, at which point they can select Whatsapp to share to), follow their instructions:

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

You need to save the image to disk, and then create a UIDocumentInteractionController with that file URL.

Here is some example code:

_documentController = [UIDocumentInteractionController interactionControllerWithURL:_imageFileURL];
_documentController.delegate = self;
_documentController.UTI = @"net.whatsapp.image";
[_documentController presentOpenInMenuFromRect:CGRectZero inView:self.view animated:YES]
Daniel Amitay
  • 6,677
  • 7
  • 36
  • 43
  • Thank you very much. I try it and with UIDocumentInteracionController I got send image via WhatsApp. – Paolpa Oct 24 '13 at 15:21
  • Is it possible to share an image AND a text in a single share? – Vaiden Apr 06 '14 at 13:42
  • According to this post, since November you can do both. Here's the post that explains how. http://stackoverflow.com/questions/23077338/share-image-and-text-through-whatsapp-or-facebook It is for Android development, please let me know if you want me to write it for Obj-C/Swift. – Laurent Rivard Jan 27 '15 at 14:16
  • @LaurentRivard How to share both in Obj-C? I have [asked a question here](http://stackoverflow.com/questions/29741858/ios-share-image-and-text-to-whatsapp) – Js Lim Apr 20 '15 at 08:58
2

Here is a finally working solution for Swift. The method is triggered by a button. you can find some more explanations here

import UIKit

class ShareToWhatsappViewController: UIViewController, UIDocumentInteractionControllerDelegate {
    var documentController: UIDocumentInteractionController!
    override func viewDidLoad() {
        super.viewDidLoad()
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
    }

    @IBAction func shareToWhatsapp(sender: UIButton) {
        let image = UIImage(named: "my_image") // replace that with your UIImage

        let filename = "myimage.wai"
        let documentsPath = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, false)[0] as! NSString
        let destinationPath = documentsPath.stringByAppendingString("/" + filename).stringByExpandingTildeInPath
        UIImagePNGRepresentation(image).writeToFile(destinationPath, atomically: false)
        let fileUrl = NSURL(fileURLWithPath: destinationPath)! as NSURL

        documentController = UIDocumentInteractionController(URL: fileUrl)
        documentController.delegate = self
        documentController.UTI = "net.whatsapp.image"
        documentController.presentOpenInMenuFromRect(CGRectZero, inView: self.view, animated: false)
    }
}