1
votes

I'm developing a Web app using a WKWebView. The problem I'm facing right now is the following:

I'm using webView(_ webView: WKWebView, decidePolicyFor navigationResponse: WKNavigationResponse, decisionHandler: @escaping (WKNavigationResponsePolicy) -> Void) to determine whether the user is trying to access an attached file like a .pdf, .mov, .docx or .pages and allow or cancel the navigation using the decisionHandler.

However when I try to access the navigationResponse's response property via

if let response = navigationResponse.response as? HTTPURLResponse

the optional check fails for certain file types like .docx, .pages, .rtf. For other types like .mov everthing works as expected.

Inspecting navigationResponse.response.url for the critical navigations towards files like .rtf I find the following:

x-apple-ql-id://BB110CD9-7330-4A39-82E4-6E550E9AC4EB/x-apple-ql-magic/ein%20text.rtf

I have found out so far that this url scheme belongs to Apple's Quicklook framework which is used to display certain file contents within WKWebView to the user. However this scheme is only used internally and should not appear there.

After the optional check of navigationResponse.response and casting it to HTTPURLResponse fails, I try to cancel the navigation calling decisionHandler(.cancel). The result is my webview showing a blanc page and none of the WKWebViews error functions, nor thedidFinish` method being called.

If I try to schedule any request after this happened via webView.load(someRequest)

webView(_ webView: WKWebView, didFailProvisionalNavigation navigation: WKNavigation!, withError error: Error)

gets called with error being Error Domain=WebKitErrorDomain Code=102 aka. "Frame load interrupted".

I have no idea what causes WKWebView to show such strange behavior.

The OS is iOS 11.4.

Does anyone have an idea what might be the cause or is anyone able to give a hint where to investigate further?

Thank you very much.

1
Could you decide on a policy when the previous link is clicked, before the document loads, instead?Thomas Deniau
That is possible but I would have to posses a complete list of file types that WKWebView tries to show via Quicklook. And I can not seem to find one. Further I want to access the response's headers (for content-disposition) to determine if the file is an attachment and should therefore be downloaded or not.Mbeezy

1 Answers

0
votes
  1. I had the same issue as well on iOS 12.0 while worked fine on iOS 13.
  2. I am not sure if it is the default behavior due to QuickLook feature in WKWebView or it is a bug.

  3. I have implemented a workaround to use navigationAction.request.URL (which is could be extracted in below method) to download the file:

- (void)webView:(WKWebView *)webView decidePolicyForNavigationAction:(WKNavigationAction *)navigationAction decisionHandler:(void (^)(WKNavigationActionPolicy))decisionHandler;