I am trying to display a pdf file with the PDFKit and the NSViewRepresentable wrapper
The following code displays a grayed frame and not the contents of the selected pdf file
import SwiftUI
import AppKit
import PDFKit
struct ContentView: View {
@State var pdfURL: URL? = nil
var body: some View {
VStack {
Button {
let panel = NSOpenPanel()
panel.allowsMultipleSelection = false
panel.canChooseDirectories = false
if panel.runModal() == .OK {
if let src = panel.url {
pdfURL = src
}
}
} label: {
Text("Pick a PDF file")
}
PDFKitView(url: pdfURL)
.scaledToFit()
}
.padding()
}
}
#Preview {
ContentView()
}
struct PDFKitView: NSViewRepresentable {
let url: URL?
func makeNSView(context: NSViewRepresentableContext<PDFKitView>) -> PDFView{
let pdfView = PDFView()
if let url = url {
pdfView.document = PDFDocument(url: url)
}
return pdfView
}
func updateNSView(_ nsView: PDFView, context: NSViewRepresentableContext<PDFKitView>) {
}
}
I cannot figure out what I am doing wrong.
Here is a screenshot after picking a pdf file
Screenshot