Send image and text using Whatsapp

I need to send an image from my application with text, I know how to send only an image or just text, but I do not know how to combine them.

Simple 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) 

Text only:

  var whatsappURL = NSURL(string: "whatsapp://send?text=hello,%20world") if UIApplication.sharedApplication().canOpenURL(whatsappURL!) { UIApplication.sharedApplication().openURL(whatsappURL!) } 

How to send an image with text?

CHANGE NO. 1

I found code that uses an image with text in whatsapp, but 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(); } 
+4
source share
2 answers

You can post an image or text on WhatsApp. However, you cannot publish both at the same time, since WhatsApp does not provide an API to which you can add a title and publish an image with text.

Now there is an API for interacting with WhatsApp:

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

Also find below a helpful answer:

You can use the UIDocumentInteractionController as mentioned in the 2nd answer to this question of August 4, 2014. Image / text sharing via WhatsApp in iOS app

Hope this helps.

+3
source

Version of your sharing image code for quick 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 not working even for image sharing, but can save time

+1
source

All Articles