swift - iOS/swift : QLPreviewController shows blank page in iOS11

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

最佳答案

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

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

后即可

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

关于swift - iOS/swift : QLPreviewController shows blank page in iOS11,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47053192/

相关文章:

ios - 想要将 API 数据显示到标签(Swift、Alamofire)

UIScrollView contentLayoutGuide 和缩放居中

ios11 didUpdateLocations 未调用

快速显示 QLPreviewPanel

view - 在 XCode 4.5 中正确使用新的 "container view"?

swift - [错误] : object not found for update Swift + Parse. com

ios - 在 Swift 3 中按字母顺序对 Realm 对象排序

php - 使用 jquery/php/html 更改 img src 和 fileupload 值

regex - Swift 中特定字符后用空格分隔值

iphone - ViewController 不调用 deinit 方法 swift 4