From 5cf4a2e5b91490325c10a21b3dc951fc07028811 Mon Sep 17 00:00:00 2001 From: Stefan Lange-Hegermann Date: Wed, 12 Aug 2026 11:25:52 +0200 Subject: [PATCH] Fix Quick Look preview on iOS .quickLookPreview() is a macOS-only SwiftUI modifier, so the app target did not compile for iOS at all. Present QLPreviewController through a UIViewControllerRepresentable in a sheet instead; clearing previewURL still triggers cleanup and the review prompt. --- Cable/Loads/LoadsView.swift | 12 +++++++- Cable/Loads/QuickLookPreview.swift | 48 ++++++++++++++++++++++++++++++ 2 files changed, 59 insertions(+), 1 deletion(-) create mode 100644 Cable/Loads/QuickLookPreview.swift diff --git a/Cable/Loads/LoadsView.swift b/Cable/Loads/LoadsView.swift index ff013e8..d0ae6b3 100644 --- a/Cable/Loads/LoadsView.swift +++ b/Cable/Loads/LoadsView.swift @@ -220,7 +220,17 @@ struct LoadsView: View { } ) } - .quickLookPreview($previewURL) + .sheet( + isPresented: Binding( + get: { previewURL != nil }, + set: { if !$0 { previewURL = nil } } + ) + ) { + if let previewURL { + QuickLookPreview(url: previewURL) + .ignoresSafeArea() + } + } .onChange(of: previewURL) { _, newValue in if newValue == nil { cleanupPreview() diff --git a/Cable/Loads/QuickLookPreview.swift b/Cable/Loads/QuickLookPreview.swift new file mode 100644 index 0000000..f396f58 --- /dev/null +++ b/Cable/Loads/QuickLookPreview.swift @@ -0,0 +1,48 @@ +// +// QuickLookPreview.swift +// Cable +// + +import QuickLook +import SwiftUI + +/// Presents a single exported file (PDF or PNG) in Quick Look so the user can +/// inspect it before sharing. +/// +/// SwiftUI's `quickLookPreview(_:)` modifier is macOS-only, so iOS wraps +/// `QLPreviewController` directly. +struct QuickLookPreview: UIViewControllerRepresentable { + let url: URL + + func makeUIViewController(context: Context) -> QLPreviewController { + let controller = QLPreviewController() + controller.dataSource = context.coordinator + return controller + } + + func updateUIViewController(_ controller: QLPreviewController, context: Context) { + guard context.coordinator.url != url as NSURL else { return } + context.coordinator.url = url as NSURL + controller.reloadData() + } + + func makeCoordinator() -> Coordinator { + Coordinator(url: url as NSURL) + } + + // MARK: - Coordinator + + final class Coordinator: NSObject, QLPreviewControllerDataSource { + var url: NSURL + + init(url: NSURL) { + self.url = url + } + + func numberOfPreviewItems(in controller: QLPreviewController) -> Int { 1 } + + func previewController(_ controller: QLPreviewController, previewItemAt index: Int) -> QLPreviewItem { + url + } + } +}