in my SwiftUI
app, I’ve a easy class that hundreds a PDF after which tries to leap to web page 2:
struct PDFKitView2: UIViewRepresentable {
let pdf: PDF
func makeUIView(context: Context) -> PDFView {
let pdfView = PDFView()
pdfView.doc = PDFDocument(information: pdf.information)
pdfView.autoScales = true
pdfView.pageShadowsEnabled = false
return pdfView
}
func updateUIView(_ uiView: PDFView, context: Context) {
print("updateUIView")
if context.coordinator.shouldRestoreDestination,
let doc = uiView.doc,
let web page = doc.web page(at: 2) {
DispatchQueue.major.async {
print("1")
uiView.go(to: web page)
context.coordinator.shouldRestoreDestination = false
}
} else {
context.coordinator.shouldRestoreDestination = false
}
}
func makeCoordinator() -> Coordinator {
Coordinator(self)
}
class Coordinator: NSObject {
var guardian: PDFKitView2
var shouldRestoreDestination: Bool
init(_ guardian: PDFKitView2) {
self.guardian = guardian
self.shouldRestoreDestination = true
}
}
}
In my app, I’ve a button referred to as “Add PDF” that calls the .fileImporter
, lets the person choose PDF, shops the info in a mannequin object referred to as PDF, then launches in it the above view. Once I do it this manner, the above “go to web page” code works effective. Nonetheless, when the person tries to launch the identical saved PDF object from a listing, it doesn’t bounce to web page 2:
Not understanding the issue right here, presumably some situation with loading the info? Full code is beneath:
import SwiftUI
import SwiftData
import PDFKit
@Mannequin
closing class PDF {
var fileName: String = ""
var information: Information = Information()
init(fileName: String, information: Information) {
self.fileName = fileName
self.information = information
}
}
struct HomeView: View {
@Setting(.dismiss) non-public var dismiss
@Setting(.modelContext) non-public var modelContext
@Question non-public var pdfs: [PDF]
@State non-public var pdfPopoverListIsPresented = false
@State non-public var pdfToPresent: PDF?
@State non-public var filesImporterIsPresented = false
@State non-public var selectedFileURL: URL?
var physique: some View {
HStack {
Button("Add PDF") {
filesImporterIsPresented = true
}
Button("(pdfs.depend)") {
pdfPopoverListIsPresented = true
}
.popover(isPresented: $pdfPopoverListIsPresented) {
VStack {
ForEach(pdfs) { pdf in
Button {
pdfToPresent = pdf
} label: {
Textual content("(pdf.fileName).pdf")
}
}
}
.padding()
}
}
.fileImporter(
isPresented: $filesImporterIsPresented,
allowedContentTypes: [.pdf],
allowsMultipleSelection: false
) { end in
if let urls = attempt? consequence.get(), let url = urls.first {
selectedFileURL = url
} else {
print("Error deciding on file")
}
}
.fullScreenCover(merchandise: $pdfToPresent) { pdfToPresent in
VStack {
PDFKitView2(pdf: pdfToPresent)
Button("Shut") {
pdfToPresent = nil
}
}
}
.onChange(of: selectedFileURL) {
if let selectedFileURL {
if let pdf = pdf(from: selectedFileURL) {
modelContext.insert(pdf)
pdfToPresent = pdf
}
selectedFileURL = nil
}
}
.activity {
do {
attempt modelContext.delete(mannequin: PDF.self)
} catch {
fatalError(error.localizedDescription)
}
}
}
non-public func pdf(from url: URL) -> PDF? {
if url.startAccessingSecurityScopedResource() {
defer { url.stopAccessingSecurityScopedResource() }
let ext = url.pathExtension.lowercased()
if ext == Constants.fileExtensionPDF || ext == Constants.fileExtensionPlainText {
if let information = attempt? Information(contentsOf: url) {
return PDF(
fileName: url.deletingPathExtension().lastPathComponent,
information: information
)
}
}
}
return nil
}
}
struct PDFKitView2: UIViewRepresentable {
let pdf: PDF
func makeUIView(context: Context) -> PDFView {
let pdfView = PDFView()
pdfView.doc = PDFDocument(information: pdf.information)
pdfView.autoScales = true
pdfView.pageShadowsEnabled = false
return pdfView
}
func updateUIView(_ uiView: PDFView, context: Context) {
print("updateUIView")
if context.coordinator.shouldRestoreDestination,
let doc = uiView.doc,
let web page = doc.web page(at: 2) {
DispatchQueue.major.async {
print("1")
uiView.go(to: web page)
context.coordinator.shouldRestoreDestination = false
}
} else {
context.coordinator.shouldRestoreDestination = false
}
}
func makeCoordinator() -> Coordinator {
Coordinator(self)
}
class Coordinator: NSObject {
var guardian: PDFKitView2
var shouldRestoreDestination: Bool
init(_ guardian: PDFKitView2) {
self.guardian = guardian
self.shouldRestoreDestination = true
}
}
}