I have a SwiftUI iOS app that I have made available for mac catalyst. I am using QuickLook Preview to dispaly pdf, docx, images and pptx in the app. The rendering works fine on iOS but on MacOS, only 1 page of the document is displayed with a blurry look.
Here is my code that implements the QuickLook:
import SwiftUI
import QuickLook
struct PreviewController: UIViewControllerRepresentable {
@Binding var url: URL
func makeUIViewController(context: Context) -> QLPreviewController {
let controller = QLPreviewController()
controller.dataSource = context.coordinator
return controller
}
func updateUIViewController(
_ uiViewController: QLPreviewController, context: Context) {}
func makeCoordinator() -> Coordinator {
return Coordinator(parent: self)
}
class Coordinator: QLPreviewControllerDataSource {
let parent: PreviewController
init(parent: PreviewController) {
self.parent = parent
}
func numberOfPreviewItems(in controller: QLPreviewController) -> Int {
return 1
}
func previewController(_ controller: QLPreviewController, previewItemAt index: Int) -> QLPreviewItem {
return parent.url as NSURL
}
}
}
and then I calling it in my Content view in a sheet. Question is what do I do differently to render the documents properly on the MacOS?
I finally found a working solution to this problem.The trick is to display the quicklook preview in a navigationViewController. here is the code:
struct PreviewControllerMac: UIViewControllerRepresentable {
@Binding var url: URL
func makeUIViewController(context: Context) -> UINavigationController {
let controller = QLPreviewController()
controller.dataSource = context.coordinator
controller.delegate = context.coordinator
let navigationController = UINavigationController(rootViewController: controller)
return navigationController
}
func updateUIViewController(_ uiViewController: UINavigationController, context: Context) {}
func makeCoordinator() -> Coordinator {
return Coordinator(parent: self)
}
class Coordinator: NSObject, QLPreviewControllerDelegate, QLPreviewControllerDataSource {
let parent: PreviewControllerMac
init(parent: PreviewControllerMac) {
self.parent = parent
}
func numberOfPreviewItems(in controller: QLPreviewController) -> Int {
return 1
}
func previewController(_ controller: QLPreviewController, previewItemAt index: Int) -> QLPreviewItem {
return parent.url as NSURL
}
func previewController(_ controller: QLPreviewController, editingModeFor previewItem: QLPreviewItem) -> QLPreviewItemEditingMode {
return .updateContents
}
}