HomeiOS Developmentios - SwiftUI: Tips on how to show a PDF file after...

ios – SwiftUI: Tips on how to show a PDF file after which change it to a distinct file?


I’ve this code that shows a PDF, and the PDF hundreds wonderful. Nevertheless, I’ve added a toolbar button to alter the PDF to a distinct file, nevertheless it would not work. I do not get any error messages, however the View just isn’t up to date to indicate the brand new file. Please, assist!

import SwiftUI
import PDFKit

struct PDFKitRepresentedView: UIViewRepresentable {
    let url: URL
    let pdfView = PDFView()
    
    init(_ url: URL) {
        self.url = url
        pdfView.doc = PDFDocument(url: self.url)
    }

    func makeUIView(context: UIViewRepresentableContext<PDFKitRepresentedView>) -> PDFKitRepresentedView.UIViewType {
        return pdfView
    }

    func updateUIView(_ uiView: UIView, context: UIViewRepresentableContext<PDFKitRepresentedView>) {
    }
}

struct PDFKitView: View {
    var url: URL
    var physique: some View {
        PDFKitRepresentedView(url)
    }
}

struct ContentView: View {
    
    @State var documentURL = Bundle.important.url(forResource: "file1", withExtension: "pdf")!
    
    var physique: some View {
        
        NavigationStack {
            PDFKitView(url: documentURL)
            .toolbar {
                Button("Change PDF") {
                    documentURL = Bundle.important.url(forResource: "file2", withExtension: "pdf")!
                }
            }
        }
    }
}

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments