通过WhatsApp分享PDF

问题描述 投票:1回答:2

我想直接与WhatsApp共享PDF。

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

Share image/text through WhatsApp in an iOS app

ios swift pdf whatsapp
2个回答
1
投票

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

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

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)
© www.soinside.com 2019 - 2024. All rights reserved.