无法快速共享 pdf(仅限 WhatsApp)

Posted

技术标签:

【中文标题】无法快速共享 pdf(仅限 WhatsApp)【英文标题】:Not able to share pdf in swift (Only WhatsApp) 【发布时间】:2019-12-12 10:57:32 【问题描述】:

我想从应用程序共享 pdf 文件。我收到一个错误“无法共享”文档一些 pdf。我在某些文件中没有收到错误。这是我的代码:

class DocumentURL: UIActivityItemProvider 
    let temporaryURL: URL
    let document: DocumentAndDemandForm
    let data : Data

    init(document: DocumentAndDemandForm,data:Data) 
        self.document = document
        self.data = data
        self.temporaryURL = URL(fileURLWithPath: NSTemporaryDirectory() + "\(document.downloadName == "" ? document.description ?? "ek" : document.downloadName).pdf")
        super.init(placeholderItem: temporaryURL)
    
    override var item: Any 
        get 
            try? data.write(to: temporaryURL)
            return temporaryURL
        
    


func share(isSave:Bool = false) 
        if let data = pdfView.document?.dataRepresentation()
            let activityViewController: UIActivityViewController = UIActivityViewController(activityItems: [DocumentURL.init(document: self.document, data: data)], applicationActivities: nil)
            activityViewController.popoverPresentationController?.sourceView = self.pdfView
            if isSave
                activityViewController.excludedActivityTypes = [.airDrop,.assignToContact,.copyToPasteboard,.mail,.message,.openInIBooks,.postToFacebook,.postToFlickr,.postToTencentWeibo,.postToTwitter,.postToVimeo,.postToWeibo]
            
            DispatchQueue.main.async 
                self.controller.present(activityViewController, animated: true, completion: 
                    self.shareDocument = false

                )
            
        
    

【问题讨论】:

【参考方案1】:

检查我的简单代码可能对您有帮助,不要忘记为 whatsapp 配置共享设置,只有它才会出现在共享实例中..

@objc func shareTapped() 

        do 
            let data = try Data(contentsOf: URL(fileURLWithPath: documentPath!))
            let activityViewController: UIActivityViewController = UIActivityViewController(activityItems: [docTitle!,data], applicationActivities: nil)
            activityViewController.popoverPresentationController?.sourceView = self.view

            self.present(activityViewController, animated: true, completion: nil)
            print("pdf file loading...")
        
        catch 
            let msgView = Utils.swiftMeassageView(title: "", message: "PDF file not found.", theme: .info)
            SwiftMessages.show(view: msgView)
        
    

【讨论】:

【参考方案2】:

在您的Info.plist 中添加这两个键:

文件共享已启用,UIFileSharingEnabled:此密钥(应用程序支持 iTunes 文件共享)将启用 iTunes 共享您 Documents 文件夹中的文件。确保将布尔值设置为 true。 LSSupportsOpeningDocumentsInPlace:此密钥(支持就地打开文档)将授予应用程序文件提供者读取或写入文档文件夹中文件的权限。确保将布尔值设置为 YES。
let fileManager = FileManager.default
let documentoPath = getDirectoryPath().appendingPathComponent(url.lastPathComponent) as URL
print("documentoPath :\(documentoPath)")
if fileManager.fileExists(atPath: documentoPath.path) 
    DispatchQueue.main.async 
        let activityViewController: UIActivityViewController = UIActivityViewController(activityItems: [documentoPath], applicationActivities: nil)
        activityViewController.popoverPresentationController?.sourceView = self.view
        self.downloadNavController?.present(activityViewController, animated: true, completion:  () in
            UIBarButtonItem.appearance().tintColor = UIColor.systemBlue
            UINavigationBar.appearance().barTintColor = UIColor.white
        )
    
 else 
    print("document was not found")

【讨论】:

以上是关于无法快速共享 pdf(仅限 WhatsApp)的主要内容,如果未能解决你的问题,请参考以下文章

无法通过whatsapp分享pdf

无法使用 UIActivityViewController 共享到 whatsapp 的超链接

ActivityViewController 无法在 WhatsApp 和 Mail App 上共享链接?

如何使用 Swift 从 iOS 应用程序共享音频/图像到 whatsapp

如何分享网络连接给whatsapp

无法从我的网站在 Whatsapp 上分享