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.
This commit is contained in:
2026-08-12 11:25:52 +02:00
parent 9257da046a
commit 5cf4a2e5b9
2 changed files with 59 additions and 1 deletions

View File

@@ -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 .onChange(of: previewURL) { _, newValue in
if newValue == nil { if newValue == nil {
cleanupPreview() cleanupPreview()

View File

@@ -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
}
}
}