通过WhatsApp分享PDF

时间:2018-02-28 06:01:51

标签: ios swift pdf whatsapp

我想直接与WhatsApp共享PDF。

以下是我在iOS应用中发现在WhatsApp中发送文字和图片的链接,但无法找到如何将PDF直接发送到WhatsApp。

Share image/text through WhatsApp in an iOS app

2 个答案:

答案 0 :(得分:1)

您可以使用Share Extension(UIActivityViewController)来共享您的pdf fileURL。请注意,用户必须选择WhatsApp应用程序来共享文件。如果您想先安装WhatsApp,请注意编辑info.plist并将whatsapp添加到LSApplicationQueriesSchemes数组:

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

答案 1 :(得分:0)

首先,您必须以Data格式检索PDF文件。

var pdfDATA:Data!

获取上述变量的数据后,您可以在代码下方运行以通过WhatsApp发送PDF。

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)