I am using an instance of UIDocumentInteractionController
to offer the user the option to open a given document, if a capable app is installed on their device.
Apple's documentation for the QuickLook Framework mentions that:
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.
(emphasis mine)
I am opting for that third option: Instead of using a QLPreviewController
, I am presenting an UIDocumentInteractionController
; this is my code:
@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)
}
}
The fallback path gets executed (options menu), because I don't have any app that can open docx. However, the mentioned "Quick Look" option is not present:
What am I missing?
NOTE: I am not implementing any of the methods of UIDocumentInteractionControllerDelegate
.
Silly me... again.
ANSWER: It turns out that, to have the QuickLook option present, you need to implement this method of the delegate protocol:
func documentInteractionControllerViewControllerForPreview(controller: UIDocumentInteractionController) -> UIViewController {
return self // returning self seems to work
}
(I somehow missed this. On a first read, I thought implementing this method meant that I should return a view controller capable of displaying the content -i.e. a full-fledged docx renderer, in this case. It just asks for a "source" from which to show the preview)
Once I implemented that method, the eye button started to appear in the options menu.
But on clicking it, my app would crash: by the time the quick look launches, the UIDocumentInteractionController
was deallocated. I changed it from local variable to property, and now it works.