UIDocumentInteractionController - 显示 "Quick Look" 选项

UIDocumentInteractionController - Show "Quick Look" Option

我正在使用 UIDocumentInteractionController 的实例为用户提供打开给定文档的选项,前提是他们的设备上安装了功能强大的应用程序。

Apple's documentation for the QuickLook Framework 提到:

To display a Quick Look preview controller you can use any of these options:

  • Push it into view using a UINavigationController object.
  • Present it modally, full screen, using the presentModalViewController:animated: method of its parent class, UIViewController.
  • Present a document interaction controller (as described in Previewing and Opening Files. The user can then invoke a Quick Look preview controller by choosing Quick Look from the document interaction controller’s options menu.

(强调我的)

我选择了第三个选项:我没有使用 QLPreviewController,而是提出了 UIDocumentInteractionController;这是我的代码:

@IBAction func openDocument(sender: AnyObject) {

    let interactionController = UIDocumentInteractionController(URL: documentURL)
    interactionController.delegate = self

    // First, attempt to show the "Open with... (app)" menu. Will fail and
    // do nothing if no app is present that can open the specified document
    // type.

    let result = interactionController.presentOpenInMenuFromRect(
        self.view.frame,
        inView: self.view,
        animated: true
    )

    if result == false {
        // Fall back to options view:

        interactionController.presentOptionsMenuFromRect(
            self.view.frame,
            inView: self.view,
            animated: true)
    }
}

回退路径被执行(选项菜单),因为我没有任何可以打开 docx 的应用程序。 但是,提到的 "Quick Look" 选项不存在:

我错过了什么?


注意: 我没有实现 UIDocumentInteractionControllerDelegate.

的任何方法

傻我...又是

答案:事实证明,要显示 QuickLook 选项,您需要实现委托协议的这个方法:

func documentInteractionControllerViewControllerForPreview(controller: UIDocumentInteractionController) -> UIViewController {
    return self // returning self seems to work
}

(我不知何故错过了这个。初读时,我认为实现这个方法意味着我应该 return 一个能够显示内容的视图控制器 - 即一个完整的 docx渲染器,在这种情况下。它只要求 "source" 从中显示预览)

实施该方法后,眼睛按钮开始出现在选项菜单中。

但是在单击它时,我的应用程序会崩溃:在快速查看启动时,UIDocumentInteractionController 已被释放。我将它从局部变量更改为 属性,现在可以使用了。