How to download a file/Data after verifying the “Content_Type” from **decidePolicyForNavigationResponse** in WKWebview?

How to retrieve the actual file/Data after doing some check inside this decidePolicyForNavigationResponse delegate method?


- (void)webView:(WKWebView *)webView decidePolicyForNavigationResponse:(WKNavigationResponse *)navigationResponse decisionHandler:(void (^)(WKNavigationResponsePolicy))decisionHandler

{

NSHTTPURLResponse *response = (NSHTTPURLResponse *)navigationResponse.response;


}

I’m not 100% sure I understand your question, but I believe your goạl is to use the

-webView:decidePolicyForNavigationResponse:decisionHandler:
delegate callback to learn about the user navigating to a specific type of resource and then, when they do, download the resource in native code. If so, the basic approach is:
  1. Grab the URL and any other relevant details from the WKNavigationResponse

  2. Resolve the navigation policy request by calling

    decisionHandler
    with
    WKNavigationResponsePolicyCancel
  3. Start a native request to download the URL (typically using NSURLSession) you got it step 1

Share and Enjoy

Quinn “The Eskimo!”
Apple Developer Relations, Developer Technical Support, Core OS/Hardware

let myEmail = "eskimo" + "1" + "@apple.com"

Yaa its an correct way, but i am getting

Error: status code: 401


The requset maybe losing its session.


In previous webkit(webview) we have navigation policies "listener download" ,"listener use", "listener ignore", the "listener download" can download file without losing session. But WKWebview has no such option. So i am strugging to perform download file from an URL. Please help me to find a solution.

Are you trying to solve this problem in general? Or just for some specific site, or set of sites?

Share and Enjoy

Quinn “The Eskimo!”
Apple Developer Relations, Developer Technical Support, Core OS/Hardware

let myEmail = "eskimo" + "1" + "@apple.com"

I need to solve this problem for a specific site https://web.groupme.com/


The issue is downloading of documents (.Doc, .Docx, .xls) not happening and giving an error status code 401.

But the image files in from same site getting downloaded properly.

To start, error 401 means “Unauthorized”, meaning that the request failed but could succeed if you authenticated properly.

Web sites usually authenticate requests in one of three ways:

  • Cookies

  • HTTP authentication (Basic, Digest, and so on)

  • Some custom scheme, like a custom HTTP header

The first approach is by far the most common, but you’ll have to confirm which approach is in play before moving on. There may be helpful hints in the 401 response itself (for example, a

WWW-Authenticate
header), or you may need to look at how the web browser is doing its authentication.

If, as I suspect, the site is using cookies, you’ll have to copy the cookies from the web view to your native request. This can be a bit tricky. There is, alas, no way to set up an WKWebView to share its cookie context with an NSURLSession (although that would make a fine enhancement request; if you do file such a request, please post your bug number, just for the record). In most cases you can get the required cookies from a web view by running JavaScript code in that web view (using

-evaluateJavaScript:completionHandler:
) to return the JavaScript
document.cookie
value.

Share and Enjoy

Quinn “The Eskimo!”
Apple Developer Relations, Developer Technical Support, Core OS/Hardware

let myEmail = "eskimo" + "1" + "@apple.com"
How to download a file/Data after verifying the “Content_Type” from **decidePolicyForNavigationResponse** in WKWebview?
 
 
Q