2

The WKWebView is not loading links. I am linking users to a privacy policy page, and the page has a group of links. The links are all pdfs hosted by wix. On safari and Chrome it works, but not on WKWebView. When the page loads, and you click the links, I just get an error:

Unknown result for URL 0x28157d110 (https)

This is how I'm loading the web view...

webView.load(URLRequest(url: URL(string: "https://mywebsite.io/legal")!))

EDIT: This is different from other questions because I have no intention of downloading the pdf - I just want to display it the same way that Safari does.

EDIT: I just replaced WKWebView with UIWebView (deprecated) and the pdfs load. The issue is with WKWebView. The Pdfs are ssl-enabled https ->

let req = URLRequest(url: URL(string: "https://mywebsite.io/legal")!)
    legacyWebView.loadRequest(req)

EDIT: There is a page here How to open a Link to a PDF with wkwebview that suggests that you must know the link URL before opening the pdf, I don't think this is true though.

EDIT: I have 2 delegate methods implemented, including the one suggested below by @Kiril. Links to pdfs still do not open.

func webView(_ webView: WKWebView, decidePolicyFor navigationAction: WKNavigationAction, decisionHandler: @escaping (WKNavigationActionPolicy) -> Void) {
        decisionHandler(WKNavigationActionPolicy.allow)
    }
    func webView(_ webView: WKWebView, decidePolicyFor navigationResponse: WKNavigationResponse, decisionHandler: @escaping (WKNavigationResponsePolicy) -> Void) {
        decisionHandler(WKNavigationResponsePolicy.allow)
    }
Code Wiget
  • 1,540
  • 1
  • 23
  • 35
  • Possible duplicate of [Downloading files WKWebView ios](https://stackoverflow.com/questions/34547161/downloading-files-wkwebview-ios) – timbre timbre Oct 11 '19 at 17:28
  • @KirilS. I'm not trying to download them, I just want to display them like safari does, unless that is what you mean – Code Wiget Oct 11 '19 at 17:41
  • it gives you a bunch of ideas of what it might be, for example I would put my money on you trying to download PDFs from http, while your URL is https (see one fo the responses there). WebView doesn't like things like that – timbre timbre Oct 11 '19 at 17:49
  • Both are https. – Code Wiget Oct 11 '19 at 17:51
  • what if you try to loosen permissions, e.g. set `NSAllowsArbitraryLoads` or related key `NSAllowsArbitraryLoadsInWebContent` https://developer.apple.com/documentation/bundleresources/information_property_list/nsapptransportsecurity/nsallowsarbitraryloads If it works, it will be a proof that something in security of those links is not right If it doesn't, means look in other direction – timbre timbre Oct 24 '19 at 22:57
  • actually the general problem is in webview you have only 1 tab to show to the user, when you touched pdf or something like URL, if browser opens it on another tab, you don't show them for your current tab in webview. For solving this problem check it out dirtbag's answer. But remember, you have only 1 tab, if the opened URL has no return action to back your website, you must back to the app, that's all. – asilturk Jul 02 '20 at 09:53

5 Answers5

10

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

Tomer Even
  • 4,820
  • 2
  • 30
  • 36
  • Number 2 was my issue! Coming back to an old project, and I'm sure it worked previously. But now building with XCode 12 and the link on WKWebView page would not open without a long press! Switching to HTTPS fixed the problem! – firecall May 04 '21 at 03:48
3

Likely, you are using target="_blank" in your anchor tag. That opens up a new window to display the link. WKWebView is blocking your attempt to open a new window (at least by default).

The code below still does not create a new window, but instead opens the PDF, etc link in the current WKWebView. The other option seems to be to create a new WKWebView and return it, so that ios can open the link in that. I don't want extra Views being created by every click on a website inside the WKWebView.

In your ViewController.viewDidLoad

webView.uiDelegate = self

Then add the extension for the delegate

extension ViewController: WKUIDelegate {

    /**
     * Force all popup windows to remain in the current WKWebView.
     * By default, WKWebView is blocking new windows from being created
     * ex <a href="link" target="_blank">text</a>.
     * This code catches those popup windows and displays them in the current WKWebView.
     */
    func webView(_ webView: WKWebView, createWebViewWith configuration: WKWebViewConfiguration, for navigationAction: WKNavigationAction, windowFeatures: WKWindowFeatures) -> WKWebView? {

        // open in current view
        webView.load(navigationAction.request)

        // don't return a new view to build a popup into (the default behavior).
        return nil;
    }
}
dirtbag
  • 121
  • 1
  • 4
  • 1
    Even though my URLs did not have a target="_blank", my WKWebview would still not load them in place. After implementing this delegate, everything works as expected. Thanks a lot! – Lucas P. Jul 06 '20 at 10:37
1

One thing I can suggest (not sure if it will help, but too long for comment) is try to implement WKNavigationDelegate's decidePolicyFor:

// WKNavigationDelegate
func webView(_ webView: WKWebView, decidePolicyFor navigationAction: WKNavigationAction, decisionHandler: @escaping (WKNavigationActionPolicy) -> Void) {
    decisionHandler(.allow)
}

If it helps, then make that function more granular, i.e. which navigation you need to enable just for PDFs, not everything else in the world.

timbre timbre
  • 12,648
  • 10
  • 46
  • 77
0

First you should add this to your delegate:

func webView(_ webView: WKWebView, decidePolicyFor navigationAction: WKNavigationAction, decisionHandler: @escaping (WKNavigationActionPolicy) -> Void) {
     decisionHandler(WKNavigationActionPolicy.allow)
}

Then you may also need to implement the following method if the link has target="_blank"

func webView(_ webView: WKWebView, createWebViewWith configuration: WKWebViewConfiguration, for navigationAction: WKNavigationAction, windowFeatures: WKWindowFeatures) -> WKWebView? {
htafoya
  • 18,261
  • 11
  • 80
  • 104
-1

I still don't know what the true cause was, but when I changed the hosting of the files from Wix to S3, I stopped getting the issue.

Code Wiget
  • 1,540
  • 1
  • 23
  • 35
  • Sometimes it is a combination of factors, if the links are placed as target `_blank` or with `download` keywords in the HTML link. – htafoya Jan 28 '20 at 05:11