Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Send Image and Text With Whatsapp

I need to send an image from my app with a text, I know how to send just an image or just a text, but I don't know how to combine both of them.

Just an Image:

    let image = UIImage(named: "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)    

Just a text:

    var whatsappURL = NSURL(string: "whatsapp://send?text=hello,%20world")

    if UIApplication.sharedApplication().canOpenURL(whatsappURL!) {
        UIApplication.sharedApplication().openURL(whatsappURL!)
    }    

How can I send an image with a text?

EDIT #1

I found a code that share an image with text to whatsapp but it's in java, can you translate it to swift?

Intent whatsappIntent = new Intent(android.content.Intent.ACTION_SEND);
whatsappIntent.setType("image/*");
whatsappIntent.putExtra(Intent.EXTRA_TEXT, "Hello World");
whatsappIntent.putExtra(Intent.EXTRA_STREAM, Uri.parse("file://" + file)); //add image path
startActivity(Intent.createChooser(share, "Share image using"));
try {
    activity.startActivity(whatsappIntent);
} catch (android.content.ActivityNotFoundException ex) {
    Toast.makeText(activity, "Whatsapp have not been installed.", Toast.LENGTH_SHORT).show();
}    
like image 442
Omer N. Avatar asked Aug 10 '15 12:08

Omer N.


2 Answers

You can post Image or Text on WhatsApp. However you can't post both at a time as whatsapp does not provide any API that you can add caption and post image with text.

Now there is an api available for interacting with WhatsApp:

http://www.whatsapp.com/faq/en/iphone/23559013

Also Find below helpful answer:

You can use the UIDocumentInteractionController as mentioned in the 2nd answer to this question as of August 4, 2014: Share image/text through WhatsApp in an iOS app

Hope this will help.

like image 85
Niraj Avatar answered Sep 22 '22 00:09

Niraj


A version of your share image code for swift 3:

let image = myUIImageVariable
        let filename = "myimage.wai"
        let documentsPath = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, false)[0] as NSString
        var destinationPath = documentsPath.appending("/" + filename) as NSString
         destinationPath = destinationPath.expandingTildeInPath as NSString

        let fileUrl = NSURL(fileURLWithPath: destinationPath as String) as NSURL
        do{
            try UIImagePNGRepresentation(image!)?.write(to: fileUrl as URL, options: Data.WritingOptions.atomic)
        }
        catch {}
        let documentController = UIDocumentInteractionController(url: fileUrl as URL)
        documentController.delegate = self
        documentController.uti = "net.whatsapp.image"
        documentController.presentOpenInMenu(from: CGRect.zero, in: self.view, animated: false)

Still does not seem work even for just sharing an image, but may save someone's time

like image 35
jugutier Avatar answered Sep 23 '22 00:09

jugutier