QLPreviewController无法在iOS11上运行

问题描述 投票:4回答:3
class ViewController: UIViewController {
    let quickLookController = QLPreviewController()

    override func viewDidLoad() {
        super.viewDidLoad()

        quickLookController.dataSource = self
        quickLookController.delegate = self
    }

    @IBAction func buttonAction(_ sender: Any) {
        present(quickLookController, animated: true, completion: nil)

        quickLookController.reloadData()
        quickLookController.refreshCurrentPreviewItem()
    }
}

extension ViewController: QLPreviewControllerDataSource,QLPreviewControllerDelegate {

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

    func previewController(_ controller: QLPreviewController, previewItemAt index: Int) -> QLPreviewItem {
        let path = Bundle.main.path(forResource: "AppCoda-Ppt.ppt", ofType: nil)

        let url = NSURL(fileURLWithPath: path!)

        return url
    }
}

ios swift quicklook qlpreviewcontroller
3个回答
4
投票

这是iOS 11.2中的一个错误。如果您想了解其状态,请在bugreport.apple.com上提交。解决方法是不将ppt文件存储在应用包中。在容器中的某个位置使用其他位置,例如Application Support目录。


0
投票

您必须将文件复制到ie临时目录并从那里读取文件。


0
投票

来自@Thomas的回答是正确的(谢谢!)对于Swift 3,您可以在previewItemAt:index方法中执行类似的操作,以按需缓存文件:

func previewController(_ controller: QLPreviewController, previewItemAt index: Int) -> QLPreviewItem {

    guard let CacheDirURL = try? FileManager.default.url(for: .cachesDirectory, in: .userDomainMask, appropriateFor: nil, create: true) else {
        print("Can't get cache dir URL")
        return NSURL(fileURLWithPath: "FILE_NOT_FOUND")
    }

    let fileUrl = CacheDirURL.appendingPathComponent("cachedFileName.pdf")

    if !FileManager.default.fileExists(atPath: fileUrl.path) {
        if let sourceUrl = Bundle.main.url(forResource: "Pioneer_UAS_Policy_Aug_2016", withExtension: "pdf") {
            print("Copying file from bundle \(sourceUrl.path)")
            try? FileManager.default.copyItem(at: sourceUrl, to: fileUrl)
        }
    }

    return NSURL(fileURLWithPath: fileUrl.path)

}
© www.soinside.com 2019 - 2024. All rights reserved.