Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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)  
like image 358
Atif Khan Avatar asked Sep 24 '19 05:09

Atif Khan


People also ask

Is WKWebView the same as Safari?

WKWebView - This view allows developers to embed web content in your app. You can think of WKWebView as a stripped-down version of Safari. It is responsible to load a URL request and display the web content. WKWebView has the benefit of the Nitro JavaScript engine and offers more features.

How do I download files from WKWebView?

With func webView(_ webView: WKWebView, decidePolicyFor navigationAction: WKNavigationAction, decisionHandler: @escaping (WKNavigationActionPolicy) -> Void) { , you get the file url to download. Then download it with JS.


1 Answers

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)
    }
}
like image 174
Atif Khan Avatar answered Oct 09 '22 10:10

Atif Khan