iOS / Swift:QLPreviewController在iOS11

时间:2017-11-01 10:51:27

标签: swift ios11 preview quicklook qlpreviewcontroller

我已尝试在我的iOS项目中使用QLPreviewController进行文件(.txt,.pdf,.docx,.xlsx等)预览,但它在iOS 11中效果不佳:它显示空白页无论使用何种设备或模拟器。在iOS 10.3.1(模拟器)中,它运行良好,一切都很好。

以下是详细信息:

环境:

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)尝试代码,结果应该在我完成后更新到这里。

1 个答案:

答案 0 :(得分:2)

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

QLController.didMove(toParentViewController: self)添加到self.view

后,只需添加{{1}}即可

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