ios – PDFKit go(:to) not working in SwiftUI when loading information from a saved mannequin object

0
1
ios – PDFKit go(:to) not working in SwiftUI when loading information from a saved mannequin object


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.principal.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 mum or dad: PDFKitView2
        var shouldRestoreDestination: Bool
        
        init(_ mum or dad: PDFKitView2) {
            self.mum or dad = mum or dad
            self.shouldRestoreDestination = true
        }
    }
}

In my app, I’ve a button referred to as “Add PDF” that calls the .fileImporter, lets the consumer choose PDF, shops the information in a mannequin object referred to as PDF, then launches in it the above view. After I do it this manner, the above “go to web page” code works advantageous. Nevertheless, when the consumer tries to launch the identical saved PDF object from an inventory, it doesn’t soar to web page 2:

https://youtu.be/oXdvAo7QSrM

Not understanding the issue right here, probably some subject with loading the information? Full code is beneath:

import SwiftUI
import SwiftData
import PDFKit

 @Mannequin
    closing class PDF {
        var fileName: String = ""
        var information: Knowledge = Knowledge()
    
        init(fileName: String, information: Knowledge) {
            self.fileName = fileName
            self.information = information
        }
    }
    
    struct HomeView: View {
        @Atmosphere(.dismiss) non-public var dismiss
        @Atmosphere(.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.rely)") {
                    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
            ) { lead to
                if let urls = strive? outcome.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 {
                    strive 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 = strive? Knowledge(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.principal.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 mum or dad: PDFKitView2
            var shouldRestoreDestination: Bool
            
            init(_ mum or dad: PDFKitView2) {
                self.mum or dad = mum or dad
                self.shouldRestoreDestination = true
            }
        }
    }

LEAVE A REPLY

Please enter your comment!
Please enter your name here