Whatsapp Image Sharing iOS

Share image/text through WhatsApp in an iOS app

No this is not possible, whatsapp does not have any public API you can use.

Please note that this answer is correct for 2011 when there was no API for WhatsApp.

Now there is an api available for interacting with WhatsApp: http://www.whatsapp.com/faq/en/iphone/23559013

The Objective-C call to open one of these URLs is as follows:

NSURL *whatsappURL = [NSURL URLWithString:@"whatsapp://send?text=Hello%2C%20World!"];
if ([[UIApplication sharedApplication] canOpenURL: whatsappURL]) {
[[UIApplication sharedApplication] openURL: whatsappURL];
}

Share Image On Whatsapp Via URL

Firstly you should download an image from URL

  • Create a function to get data
func data(from url: URL, completion: @escaping (Data?, URLResponse?, Error?) -> ()) {
URLSession.shared.dataTask(with: url, completionHandler: completion).resume()
}

What to do for this ->

let urlWhats = "whatsapp://app"
if let urlString = urlWhats.addingPercentEncoding(withAllowedCharacters:CharacterSet.urlQueryAllowed) {
if let whatsappURL = URL(string: urlString) {

if UIApplication.shared.canOpenURL(whatsappURL as URL) {

// Set your image's URL into here
let url = URL(string: "https://your-image-url.com")!
data(from: url) { data, response, error in
guard let data = data, error == nil else { return }
DispatchQueue.main.async() { [weak self] in

let image = UIImage(data: data)
if let imageData = image.jpegData(compressionQuality: 1.0) {
let tempFile = URL(fileURLWithPath: NSHomeDirectory()).appendingPathComponent("Documents/whatsAppTmp.wai")
do {
try imageData.write(to: tempFile, options: .atomic)
self.documentInteractionController = UIDocumentInteractionController(url: tempFile)
self.documentInteractionController.uti = "net.whatsapp.image"
self.documentInteractionController.presentOpenInMenu(from: CGRect.zero, in: self.view, animated: true)

} catch {
print(error)
}
}
}
}

} else {
// Cannot open whatsapp
}
}
}

Image share only on whatsapp in swift 3

You can not hide all the option that are showing in the UIActivityViewController because it depends on the content you are sharing and also installed app in your device, but you can hide most of it like all default options like this way:

let activityController = UIActivityViewController(activityItems: sharingItems, applicationActivities: nil)

activityController.excludedActivityTypes = [
UIActivityType.assignToContact,
UIActivityType.print,
UIActivityType.addToReadingList,
UIActivityType.saveToCameraRoll,
UIActivityType.openInIBooks,
UIActivityType(rawValue: "com.apple.reminders.RemindersEditorExtension"),
UIActivityType(rawValue: "com.apple.mobilenotes.SharingExtension"),
]

present(activityController, animated: true, completion: nil)

OR

If you want to share only in whatapp so here is the way to do it. please refer below url:

Share image/text through WhatsApp in an iOS app

Sharing image to WhatsApp directly

Found the solution by LeoNatan@github. Works perfectly.

https://github.com/LeoNatan/LNExtensionExecutor/



Related Topics



Leave a reply



Submit