0

I have a WKWebview with a button that downloads a pdf, the button link is as follows:

<a href="https://mysite.appiancloud.com/suite/rest/a/content/latest/igB-Xg0UyTkBidWeSJGldcpdb0wlYmynKQi_fRvbd5xpczwkJJXkcYS/o"><img class= src="$imageurl"></a>

I have managed to download the PDF through certain logic, that has a starting point in decidePolicyFor method:

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

        let url = navigationAction.request.url

        if openInDocumentPreview(url!) {
                decisionHandler(.cancel)
                executeDocumentDownloadScript(webView, forAbsoluteUrl: url!.absoluteString)

        } else {
                decisionHandler(.allow)
        }
    }

This works well, and the url is the one with the PDF. However, if I click the button again I only receive null urls, so I cannot open the document again. This is the action I receive:

<WKNavigationAction: 0x102d95fa0; navigationType = -1; syntheticClickType = 0; position x = 0.00 y = 0.00 request = <NSMutableURLRequest: 0x282e151d0> { URL: about:blank }; sourceFrame = <WKFrameInfo: 0x102d502f0; webView = 0x103048200; isMainFrame = YES; request = <NSMutableURLRequest: 0x282e116d0> { URL: https://mysite.appiancloud.com/suite/sites/home }>; targetFrame = <WKFrameInfo: 0x102d577a0; webView = 0x103048200; isMainFrame = NO; request = (null)>>

If I test this on Safari Desktop the file is downloaded ok every time, however I don't find the reason why WKWebView stops receiving the url. No other delegate method is called, not even the createWebViewWith which tends to be called when target="_blank" urls are invoked.

I wonder if WKWebview cachePolicy is taking effect, but I don't find the way to avoid that url caching, or if it is being cached, to receive the event on when it is trying to load it again.

Also, as a note, if I long-click the link, the preview element holds the URL correctly.

htafoya
  • 15,392
  • 10
  • 62
  • 83

1 Answers1

0

Ok, my theory is that by telling the handler that WKNavigationAction policy is .cancel, following requests won't be processed.

What I did is to process the URL download in the createWebViewWith method, and let the policy to be always .allow.

htafoya
  • 15,392
  • 10
  • 62
  • 83