【问题标题】:PDF view not presentingPDF 视图不显示
【发布时间】:2019-09-30 18:57:31
【问题描述】:

我正在从 url 下载 PDF,然后在 Swift5 中使用 PDFKit 显示它。但是,当我点击我的 viewCell 时,它不会显示 PDF,也可能不会下载 PDF。我该如何验证?

我有两个文件,PlanogramViewController 和 ShowPDFViewController。

var pdfURL: URL!

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        if let plano = self.data[indexPath.row] as? Planogram {
            guard let url = URL(string: Plano.pdfUrl) else { return }

            let urlSession = URLSession(configuration: .default, delegate: self, delegateQueue: OperationQueue())

            let downloadTask = urlSession.downloadTask(with: url)
            downloadTask.resume()

            let pdfViewController = ShowPDFViewController()
            pdfViewController.pdfURL = self.pdfURL
            present(pdfViewController, animated: false, completion: nil)
            tableView.deselectRow(at: indexPath, animated: true)
        }

    }

extension PlanogramViewController:  URLSessionDownloadDelegate {
    func urlSession(_ session: URLSession, downloadTask: URLSessionDownloadTask, didFinishDownloadingTo location: URL) {
        print("downloadLocation:", location)
        // create destination URL with the original pdf name
        guard let url = downloadTask.originalRequest?.url else { return }
        let documentsPath = FileManager.default.urls(for: .cachesDirectory, in: .userDomainMask)[0]
        let destinationURL = documentsPath.appendingPathComponent(url.lastPathComponent)
        // delete original copy
        try? FileManager.default.removeItem(at: destinationURL)
        // copy from temp to Document
        do {
            try FileManager.default.copyItem(at: location, to: destinationURL)
            self.pdfURL = destinationURL
        } catch let error {
            print("Copy Error: \(error.localizedDescription)")
        }
    }
}

所以我没有从我的扩展程序中获得任何下载验证。它应该打印我的初始下载位置,然后将其移动到缓存中。如果它遇到错误,它也应该打印一个错误,但我在控制台中什么也没有。好像什么都没有下载一样。

这里是下载 PDF 后应该显示的 pdfViewController:

import UIKit
import PDFKit


class ShowPDFViewController: UIViewController {

    var pdfView = PDFView()
    var pdfURL: URL!

    override func viewDidLoad() {
        super.viewDidLoad()

        view.addSubview(pdfView)

        if let document = PDFDocument(url: pdfURL) {
            pdfView.document = document
        }

        DispatchQueue.main.asyncAfter(deadline: .now()+3) {
            self.dismiss(animated: true, completion: nil)
        }
    }

    override func viewDidLayoutSubviews() {
        pdfView.frame = view.frame
    }
}

当前的行为只是突出显示我点击的单元格。控制台中没有任何内容,显示屏上也没有显示任何内容。

【问题讨论】:

    标签: swift xcode pdf pdfkit swift5


    【解决方案1】:

    您需要在具有 tableView 的视图控制器中遵守 UITableViewDelegateUITableViewDataSourceDelegate 协议。

    并添加

    tableView.delegate = self
    tableView.dataSource = self
    

    在viewDidLoad()中

    然后,实现所需的方法,并添加可选方法以进一步自定义您的 tableView。

    【讨论】:

    • 我已经符合我的代码(我只是没有在帖子中包含类声明,我的错)
    猜你喜欢
    • 2019-11-26
    • 2021-09-05
    • 1970-01-01
    • 1970-01-01
    • 2018-05-29
    • 1970-01-01
    • 1970-01-01
    • 2017-09-13
    • 1970-01-01
    相关资源
    最近更新 更多