Wkwebview Does Not Load Links to Pdfs

WkWebView does not load links to pdfs

There are a few things you can check:

  1. Verify that the <a href links doesn't contain target="_blank" attribute since WKWebView doesn't know how to open the link in a new tab. see https://stackoverflow.com/a/25853806/810466 for how to work around that.

  2. Check if the link is HTTPS or update the App Transport Security Settings with the Allow Arbitrary Loads option

  3. Make sure that you start the loading request only after adding the WKWebView to the view hierarchy in didMoveToParentViewController: since it may make javascript to fail if it tries to run outside the view hierarchy

  4. Implement the WKWebView NavigationDelegate methods and make sure you return WKNavigationActionPolicyAllow when deciding the policy for the request

WKWebView not loading pdf

If you are directly trying to open the pdf from URL into WKWebView it will not work because of restriction added to WKWebView for device protection.

You can refer this article How to open a Link to a PDF with wkwebview for more information.

How to open a Link to a PDF with wkwebview

SWIFT 3.* & 4.* *

First you have to download that pdf file into your app, after downloading you have to get that file path, then that file path should be use like following way in WKWebView.

let fileURL = URL(fileURLWithPath: filePathURLData as! String)
webView.loadFileURL(fileURL, allowingReadAccessTo: fileURL)

Here filePathURLData is your actual file path which you have downloaded into your app, you have to convert this into URL, then you need to load that file into WKWebView


Hope this will help you.

This will show any file in the WKWebView (doc, docx, xlsx, pdf, google doc, pages & Any textfile)

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

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.

iOS 13 WKWebView not showing pdf file anymore

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)

WKWebView open PDF in external browser

You can do something like this

  extension ViewController: WKNavigationDelegate{
func webView(_ webView: WKWebView, decidePolicyFor navigationAction: WKNavigationAction, decisionHandler: @escaping (WKNavigationActionPolicy) -> Void) {
print(navigationAction.request.url ?? "")
if let url = navigationAction.request.url?.absoluteString{


And don't forget to assign

 webView.navigationDelegate = self

The above method will get called when ever webview tries to load a new url and as per your comment and updated question you want to check the mime type of url. For that you can refer to

  1. This SO Answer

  2. Library

Why is WKWebView not opening links with target= _blank ?

My solution is to cancel the navigation and load the request with loadRequest: again. This will be come the similar behavior like UIWebView which always open new window in the current frame.

Implement the WKUIDelegate delegate and set it to _webview.uiDelegate. Then implement:

- (WKWebView *)webView:(WKWebView *)webView createWebViewWithConfiguration:(WKWebViewConfiguration *)configuration forNavigationAction:(WKNavigationAction *)navigationAction windowFeatures:(WKWindowFeatures *)windowFeatures
if (!navigationAction.targetFrame.isMainFrame) {
[webView loadRequest:navigationAction.request];

return nil;

Related Topics

Leave a reply