【发布时间】: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