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