如何在 iOS 中显示来自网络的 pdf

How to display pdf from network in iOS

目前我正在使用 QuickLook 模块从网络打开 pdf,但它在控制台中显示了一个错误 "Couldn't issue file extension for url: https://testing-xamidea.s3.amazonaws.com/flowchart/20171103182728150973368.pdf" 的空白页面。我猜 QuickLook 只能打开本地保存的 Pdf 文件。是否可以使用 quicklook 从网络加载 pdf? .到目前为止,这是我的代码- {fileURL 包含 url 从中加载 pdf,我还设置了委托等)

extension FlowchartVC:QLPreviewControllerDelegate,QLPreviewControllerDataSource {
func numberOfPreviewItems(in controller: QLPreviewController) -> Int {
return 1
 }  
func previewController(_ controller: QLPreviewController, previewItemAt index: Int) -> QLPreviewItem {

let url : NSURL! = NSURL(string : fileURL)
return url
 }
   func previewControllerWillDismiss(_ controller: QLPreviewController) {
 self.dismiss(animated: true, completion: nil)
 }
}

您需要先将文件保存到磁盘,然后才能呈现pdf。如果文件位于远程位置,则无法使用 QuickLook 显示它。该文件保存在临时目录中。这是一个示例视图控制器,展示了它是如何完成的。

Swift 5:

import UIKit
import QuickLook

class ViewController: UIViewController, QLPreviewControllerDataSource {
    // Remote url pdf I found on google
    let itemURL = URL(string: "https://www.ets.org/Media/Tests/GRE/pdf/gre_research_validity_data.pdf")!
    var fileURL: URL?
    
    override func viewDidAppear(_ animated: Bool) {
        super.viewDidAppear(animated)
        
        let quickLookController = QLPreviewController()
        quickLookController.dataSource = self

        do {
            // Download the pdf and get it as data
            // This should probably be done in the background so we don't
            // freeze the app. Done inline here for simplicity
            let data = try Data(contentsOf: itemURL)

            // Give the file a name and append it to the file path
            fileURL = FileManager().temporaryDirectory.appendingPathComponent("sample.pdf")
            if let fileUrl = fileURL {
                // Write the pdf to disk in the temp directory
                try data.write(to: fileUrl, options: .atomic)
            }
            
            // Make sure the file can be opened and then present the pdf
            if QLPreviewController.canPreview(fileUrl as QLPreviewItem) {
                quickLookController.currentPreviewItemIndex = 0
                present(quickLookController, animated: true, completion: nil)
            }
        } catch {
            // cant find the url resource
        }
    }
    
    func numberOfPreviewItems(in controller: QLPreviewController) -> Int {
        return 1
    }
    
    func previewController(_ controller: QLPreviewController, previewItemAt index: Int) -> QLPreviewItem {
        return fileURL! as QLPreviewItem
    }
}

Swift 3:

import UIKit
import QuickLook

class ViewController: UIViewController, QLPreviewControllerDataSource {
    // Remote url pdf I found on google
    let itemURL = URL(string: "https://www.ets.org/Media/Tests/GRE/pdf/gre_research_validity_data.pdf")!
    var fileURL = URL(string: "")

    override func viewDidAppear(_ animated: Bool) {
        super.viewDidAppear(animated)

        let quickLookController = QLPreviewController()
        quickLookController.dataSource = self

        do {
            // Download the pdf and get it as data
            // This should probably be done in the background so we don't
            // freeze the app. Done inline here for simplicity
            let data = Data(contentsOf: itemURL)

            // Give the file a name and append it to the file path
            fileURL = FileManager().temporaryDirectory.appendingPathComponent("sample.pdf")
            // Write the pdf to disk
            try data?.write(to: fileURL!, options: .atomic)

            // Make sure the file can be opened and then present the pdf
            if QLPreviewController.canPreview(fileUrl as QLPreviewItem) {
                quickLookController.currentPreviewItemIndex = 0
                present(quickLookController, animated: true, completion: nil)
            }
        } catch {
            // cant find the url resource
        }
    }

    func numberOfPreviewItems(in controller: QLPreviewController) -> Int {
        return 1
    }

    func previewController(_ controller: QLPreviewController, previewItemAt index: Int) -> QLPreviewItem {
        return fileURL! as QLPreviewItem
    }
}

这是模拟器中显示的文件。使用仅包含该代码的示例项目。