ioswkwebviewnsurlios-pdfkit

Why am I not able to load valid PDF file with WKWebView?


I am trying to load PDF from URL using WKWebiew using following code and I have also added necessary delegate methods of WKWebiew.

func loadPDFDocument()
{        
    if let url = URL(string: self.contentURL) 
    {
        print("URL: \(url)")
        
        if UIApplication.shared.canOpenURL(url) {
            self.webView.navigationDelegate = self
            self.webView.load(URLRequest(url: url))
        } else {
            self.showInvalidURLError()
        }
    } else {
        self.showInvalidURLError()
    }
}  

It is loading but actual content is not showing up, instead it shows like following image:

enter image description here

Now, I have tried it with PDFKit using following code and it is loading the actual content.

func loadPDFDocument()
{
    let pdfView = self.createPdfView(withFrame: self.view.bounds)
    
    if let pdfDocument = self.createPdfDocument() {
        self.view.addSubview(pdfView)
        pdfView.document = pdfDocument
    }
}

func createPdfDocument() -> PDFDocument?
{
    if let resourceUrl = URL(string: "https://d1shcqlf263trc.cloudfront.net/Engage/Contents/LearningStore/16335111672611633500529010123TestPDFfile06Oct2021.pdf") {
        return PDFDocument(url: resourceUrl)
    }
    return nil
}

func createPdfView(withFrame frame: CGRect) -> PDFView
{
    let pdfView = PDFView(frame: frame)
    pdfView.autoresizingMask = [.flexibleWidth, .flexibleHeight]
    pdfView.autoScales = true
    return pdfView
}

The reasons why I want to load this PDF using WKWebiew are following:

  1. I can get callback once URL did finish loading (with or without error)
  2. I also need to load other types of content e.g. PPT, so I can reuse WKWebiew code.

What may be the issue due to which that PDF is not being able to load using WKWebiew. Is that issue with PDF, or with URL or the way I load it with WKWebiew?


Solution

  • The code you have provided directly does not work, and leads to the following error

    WebPageProxy::didFailProvisionalLoadForFrame: frameID=3, domain=WebKitErrorDomain, code=102

    I did not find any official apple documentation for this specific error but a simple search points to some kind of interruption.

    As an alternative, you can try to load data from the URL:

        if let data = try? Data(contentsOf: url) {
            self.webView.load(data, mimeType: "", characterEncodingName: "", baseURL: url)
        }
    

    But this leads to the weird page you have shown in your question.

    Here, the web view does not know that you are trying to show a PDF. Simply providing the content type fixes the issue.

        if let data = try? Data(contentsOf: url) {
            self.webView.load(data, mimeType: "application/pdf", characterEncodingName: "UTF8", baseURL: url)
        }
    

    So, you need to make sure the web view knows what type of content you're going to load. How you would do that for various file types you want to support is a different question you need to figure out.