Share PDF through WhatsApp

二次信任 提交于 2019-12-23 02:48:20

问题


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


回答1:


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:


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")
        }
    }
}



回答2:


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)


来源:https://stackoverflow.com/questions/49022906/share-pdf-through-whatsapp

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!