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:
@@ -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()
|
||||
|
||||
48
Cable/Loads/QuickLookPreview.swift
Normal file
48
Cable/Loads/QuickLookPreview.swift
Normal 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
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user