【问题标题】:Not able to share pdf in swift (Only WhatsApp)无法快速共享 pdf(仅限 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

                })
            }
        }
    }

【问题讨论】:

    标签: swift pdf document whatsapp uiactivityviewcontroller


    【解决方案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")
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2021-02-14
        • 1970-01-01
        • 2016-07-19
        • 1970-01-01
        • 2016-08-07
        • 2018-04-15
        • 1970-01-01
        相关资源
        最近更新 更多