我想直接与WhatsApp共享PDF。
以下是我在iOS应用程序中发现WhatsApp中发送文本和图像的链接,但无法找到如何将PDF直接发送到WhatsApp。
您可以使用Share Extension(UIActivityViewController)来共享您的pdf fileURL。请注意,用户必须选择WhatsApp应用程序来共享文件。需要注意的是编辑info.plist并将whatsapp
添加到LSApplicationQueriesSchemes
数组中,如果您想先检查是否安装了WhatsApp:
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")
}
}
}
首先,您必须以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)