swift - 使用 swift 代码将 Web View 上的 pdf 共享到其他设备

标签 swift pdf web view

我有一个显示 pdf 的 WKWebView。我想使用共享按钮将文件共享到其他设备,例如 iPad、iPhone...。我尝试在预览中显示 pdf,以便它具有 iOS 共享按钮(代码如下)。

import UIKit
import WebKit

class ShowPDFView: UIViewController, UIDocumentInteractionControllerDelegate {

@IBAction func SharePDFFile(_ sender: Any) {

        let fileName = "testPDF"
        guard let urlPath = Bundle.main.url(forResource: fileName, withExtension: "pdf") else {return}
            let controller = UIDocumentInteractionController(url: urlPath)
            controller.delegate = self
            controller.presentPreview(animated: true)

    }

 func documentInteractionControllerViewControllerForPreview(controller: UIDocumentInteractionController!) -> UIViewController! {
            return self
        }

        func documentInteractionControllerViewForPreview(controller: UIDocumentInteractionController!) -> UIView! {
            return self.view
        }

        func documentInteractionControllerRectForPreview(controller: UIDocumentInteractionController!) -> CGRect{
            return self.view.frame
        }

我遇到运行时错误。

[MC] Reading from private effective user settings.

预览未加载。有谁知道为什么吗?

最佳答案

这个功能对我有用。在 Swift 4 中

@IBAction func SharePDFFile(_ sender: Any) {

        let fm = FileManager.default

        var pdfURL = (fm.urls(for: .documentDirectory, in: .userDomainMask)).last! as URL
        pdfURL = pdfURL.appendingPathComponent("johnMilky.pdf") as URL

        //Rename document name to "myFile.pdf"
        let url = URL(fileURLWithPath: NSTemporaryDirectory()).appendingPathComponent("johnMilkyFile.pdf") as NSURL

        do {
            let data = try Data(contentsOf: pdfURL)

            try data.write(to: url as URL)

            let activitycontroller = UIActivityViewController(activityItems: [url], applicationActivities: nil)
            if activitycontroller.responds(to: #selector(getter: activitycontroller.completionWithItemsHandler))
            {
                activitycontroller.completionWithItemsHandler = {(type, isCompleted, items, error) in
                    if isCompleted
                    {
                        print("completed")
                    }
                }
            }

            //activitycontroller.excludedActivityTypes = [UIActivity.ActivityType.airDrop]
            activitycontroller.popoverPresentationController?.sourceView = self.view
            self.present(activitycontroller, animated: true, completion: nil)

        }
        catch {
            print(error)
        }


    }

关于swift - 使用 swift 代码将 Web View 上的 pdf 共享到其他设备,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56104445/

相关文章:

.Net 多语言本地化

objective-c - 异或或其他简单的文件混淆方法 ios objective c

asp.net - 直接将 .aspx 转换为 .pdf

java - 多类 Java Applet

swift - NSURLSession 中的字符串 nil

python - 使用 python PyPDF2 从 PDF 中提取图像

javascript - Canvas.js 没有显示所有标签

ios - 如何在整个 View 中将 PDFSelection 居中?

ios - 按年、月、日排序时出现问题

ios - 我们在 iOS 上有哪个唯一设备标识符在安装/删除/重新安装应用程序时不会改变