1

I want to share a PDF directly to WhatsApp.

Below is the link I found to send text and image in WhatsApp in an iOS app, but unable to find that how to send PDF directly to WhatsApp.

Share image/text through WhatsApp in an iOS app

Eric Aya
  • 69,473
  • 35
  • 181
  • 253
iOS Dev
  • 33
  • 2
  • 8
  • http://www.whatsapp.com/faq/en/iphone/23559013 already there as PDF documents (UTI: com.adobe.pdf) – Pooja M. Bohora Feb 28 '18 at 06:38
  • https://stackoverflow.com/a/17723981/6656 refer this answer and read description that is mention in answer – Himanshu Moradiya Feb 28 '18 at 06:38
  • @PoojaM.Bohora I tried with this solution here is my code for the TEXT part..I'm now trying for PDF file, but unfortunately I'm not able to do it for PDF file..Below is my code for TEXT part..can you help me out for PDF file part.. – iOS Dev Feb 28 '18 at 07:27
  • var str = "text head " str=str.addingPercentEncoding(withAllowedCharacters: (NSCharacterSet.urlQueryAllowed))! let whatsappURL = URL(string: "whatsapp://send?text=\(str)") if let aURL = whatsappURL { if UIApplication.shared.canOpenURL(aURL) { if let aURL = whatsappURL { UIApplication.shared.open(aURL, options: [:], completionHandler: nil) } } } – iOS Dev Feb 28 '18 at 07:28
  • You can only share text using that url approach. To share a pdf you need to use UIActivityController or UIDocumentInteractionController – Leo Dabus Feb 28 '18 at 07:30
  • **Custom URL Scheme Opening whatsapp:// URL with one of the following parameters, will open our app and perform a custom action: app - Opens The WhatsApp Messenger application, send - New chat composer, text - If present, this text will be pre-filled into message text input field on a conversation screen.** https://faq.whatsapp.com/en/iphone/23559013 – Leo Dabus Feb 28 '18 at 07:44

2 Answers2

3

You can use Share Extension (UIActivityViewController) to share your pdf fileURL. Note that the user will have to select the WhatsApp application to share the file. Note is is required to edit your info.plist and add whatsapp to your LSApplicationQueriesSchemes array if you would like to check first if WhatsApp is installed:

enter image description here


func sharePdfWhatsApp(url: URL) {
    let whatsappURL = URL(string:"whatsapp://app")!
    // this will make sure WhatsApp it is installed
    if UIApplication.shared.canOpenURL(whatsappURL) {
        let controller = UIActivityViewController(activityItems: [url], applicationActivities: nil)
        present(controller, animated: true) {
            print("done")
        }
    }
}
Leo Dabus
  • 229,809
  • 59
  • 489
  • 571
  • UIActivityController doesnt need adding LSApplicationQueriesSchemes - whatsapp url scheme for opening Whatsapp application. It uses Whatsapp's url scheme automatically. – Kaan Esin Sep 28 '18 at 06:50
0

First you have to retrieve your PDF file as a Data format.

var pdfDATA:Data!

Once you get data to above variable you can run below code to send PDF via WhatsApp.

self.pdfDATA = try? Data.init(contentsOf: yourFilepath)
let activitycontroller = UIActivityViewController(activityItems: [self.pdfDATA], applicationActivities: nil)
            if activitycontroller.responds(to: #selector(getter: activitycontroller.completionWithItemsHandler))
            {
                activitycontroller.completionWithItemsHandler = {(type, isCompleted, items, error) in
                    if isCompleted
                    {
                    print("completed")
                    }
            }
        }
            activitycontroller.excludedActivityTypes = [UIActivityType.airDrop]
            activitycontroller.popoverPresentationController?.sourceView = buttonItemSize
            self.present(activitycontroller, animated: true, completion: nil)
Eric Aya
  • 69,473
  • 35
  • 181
  • 253
Rakesh Patel
  • 1,673
  • 10
  • 27
  • 1
    When I create a pdf file with UIGraphicsBeginPDFContextToData on the code and try to send it as you said with pdfDATA:Data, WhatsApp gives a warning like that this type of file cant be shared. So it does not accept pdf file for sharing like that. I use UIActivityController for sharing like the codes above but it doesnt work. Anybody find a solution for sharing it? (I want to share a pdf file saved on NSDocumentDirectory of the device not a real url pdf file) – Kaan Esin Sep 28 '18 at 06:47