Trying to figure out how to work with QuickLook in SwiftUI on both iOS and macOS. I suspect that in far future, there will be some unified SwiftUI QL API, but can’t see it in sight yet, so let’s work with what we have…
How do I present and configure a QLPreviewPanel from my SwiftUI view? So far, I have this:
struct ItemView: View {
let previewPanelThing = PreviewPanelThing()
var body: some View {
Button("OSX preview") {
print("osx preview")
if let previewPanel = QLPreviewPanel.shared() {
self.previewPanelThing.updateControllerForPanel(previewPanel)
previewPanel.makeKeyAndOrderFront(self.previewPanelThing)
}
}
}
}
class PreviewPanelThing: QLPreviewPanelDataSource {
func updateControllerForPanel(_ panel: QLPreviewPanel) {
print("updating controller")
panel.updateController()
}
func numberOfPreviewItems(in panel: QLPreviewPanel!) -> Int {
print("number of items")
return 1
}
func previewPanel(_ panel: QLPreviewPanel!, previewItemAt index: Int) -> QLPreviewItem! {
print("requesting preview item")
let fileURL: URL = Bundle.main.url(forResource: "Thinking-of-getting-a-cat", withExtension: "png")!
return fileURL as QLPreviewItem
}
}
This isn’t working. I suspect this is because the QLPreviewPanel documentation says: The preview panel follows the responder chain and adapts to the first responder willing to control it.
My previewPanelThing
instance isn’t in the UI and responder chain. I’m not sure how the responder chain works in SwiftUI and how to best go about it.