iOS/Swift:QLPreviewController 在 iOS11 中显示空白页

iOS/Swift: QLPreviewController shows blank page in iOS11

我尝试在我的 iOS 项目中使用 QLPreviewController 进行文件(.txt、.pdf、.docx、.xlsx 等)预览,但它在 [=31= 中效果不佳] 11:无论使用什么设备或模拟器,它都会显示空白页。在 iOS 10.3.1(simulator) 中运行良好,一切正常。

详细信息如下:

环境:

Xcode version: 8.3.3, 9.0.1
Device: iPhone 7 Plus, Simulator
iOS version: 11.0.3(iPhone 7 Plus), 10.3.1(Simulator), 11.0.1(Simulator)
Swift version: 3.1/3.2

代码

import UIKit
import QuickLook

class QuickViewController: UIViewController {

    var url: String!
    var filePath: URL!
    var msg: Message! {
        didSet {
            url = msg.content
            // create document folder url
            let documentsURL =  FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first!
            // let tempDirectory = URL(fileURLWithPath: NSTemporaryDirectory())
            filePath = documentsURL.appendingPathComponent((msg.content as NSString).lastPathComponent)
        }
    }

    @IBOutlet weak var contentView: UIView!


    // MARK: - Initialisation

    override func viewDidLoad() {
        super.viewDidLoad()
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

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


    func loafFile() {
        if FileManager.default.fileExists(atPath: filePath.path) {
            quickLookFile()
        } else {
            AppManager_Inst.download(url: "\(url!)", destinationUrl: filePath, completion: { [unowned self](succeed, msg) in
                if succeed {
                    self.quickLookFile()
                } else {
                    print(msg)
                }
            })
        }
    }

    func quickLookFile() {

        print(filePath.path)

        /// Trying to read the file via FileManager to check if the filePath is correct or not
        let data = FileManager.default.contents(atPath: filePath.path)
        print(data)

        if QLPreviewController.canPreview(filePath as NSURL) {
            let QLController = QLPreviewController()
            QLController.delegate = self
            QLController.dataSource = self
            QLController.view.frame = self.contentView.frame
            self.view.addSubview(QLController.view)
        } else {
            print("file cannot to preview")
        }
    }

}


extension QuickViewController: QLPreviewControllerDataSource, QLPreviewControllerDelegate {

    // QLPreviewControllerDataSource

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

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

    // QLPreviewControllerDelegate
    func previewController(_ controller: QLPreviewController, shouldOpen url: URL, for item: QLPreviewItem) -> Bool {
        return true
    }

}

我不确定我遗漏了什么或这里有什么问题,请帮忙。

p.s.: 我稍后会用 iPhone 5c(iOS 10.3.1) 尝试代码,完成后结果应该会更新到这里.

您没有将当前 viewController 添加为 QLPreviewController()

的父 class

添加到self.view

后,再添加QLController.didMove(toParentViewController: self)

我认为这应该可以解决您的问题。