Search code examples
iosswiftwkwebviewios13

iOS 13 WKWebView not showing pdf file anymore


I am using WKWebView to show a pdf file from a remote url. It was working fine in iOS 12 but in iOS 13 it just shows blank screen. I hit same domain with an image url and that worked fine but it has some issues with pdf files only.

let myURL = URL(string:"somefileurl.pdf") // If I hit this url in safari, It will download a pdf file.
let myRequest = URLRequest(url: myURL!)
webViewPdf.load(myRequest)  

Solution

  • I figured out that the response' content-type was "application/octet-stream" instead of "application/pdf"

    So I load the WKWebView as:

    if let myURL = URL(string:"somefileurl.pdf") {
        if let data = try? Data(contentsOf: myURL) {
           webViewPdf.load(data, mimeType: "application/pdf", characterEncodingName: "", baseURL: myURL)
        }
    }