5

I am trying to display a PDF in a SwiftUI view.

When trying to implement other solutions here, I get the error Missing arguments for parameters 'PDFName', 'DisplayName' in call

Errors image

I assume that the way you use PDFView() has changed with iOS 15; but I can't seem to find any way to use it in SwiftUI or the docs.

Any help would be greatly appreciated.

Many thanks in advance!

Current attempt at implementation resulting in above error:

import SwiftUI
import PDFKit

struct PDFViewer: View {
    var url: URL
    
    var body: some View {
        PDFKitRepresentedView(url)
    }
}

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

    func makeUIView(context: UIViewRepresentableContext<PDFKitRepresentedView>) -> PDFKitRepresentedView.UIViewType {
        let pdfView = PDFView()
        pdfView.document = PDFDocument(url: self.url)
        pdfView.autoScales = true
        return pdfView
    }

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

struct PDFView_Previews: PreviewProvider {
    static var previews: some View {
        PDFViewer(url: Bundle.main.url(forResource: "somePDF", withExtension: "pdf"))
    }
}

  • The code that you've provided works fine -- if you're encountering an error, it's coming from something that you haven't included in your code here. – jnpdx Dec 01 '21 at 22:20
  • Hi jnpdx, yeah I thought it could be, I've updated the post with a screenshot of the 4 errors, not sure what I would be missing? – AlexMcC_0102 Dec 01 '21 at 22:31
  • If you paste what you've included into a blank Xcode project, with the exception of needing a `!` after your `Bundle.main.url` call, it all compiles fine. No errors like the screenshot you've shown. That means you have to find whatever else is in your project that's contributing. Perhaps you have *your own* view named `PDFView` that is shadowing the name of `PDFView`? – jnpdx Dec 01 '21 at 22:33
  • 1
    Amazing got it, I'd defined a view as PDFView() in another page, many thanks for your help, sorry it was a stupid question in the end! – AlexMcC_0102 Dec 01 '21 at 22:52

0 Answers0