Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Retrieve HTTP response headers from WKWebview

I need to read the response HTTP headers from a WKWebview's request. I need to perform customizations based on certain custom headers sent by the server. It's not possible to add this information in the response data.

I could not find any entry in the documentation nor in previous questions here. Is there any way to do that?

like image 972
badger_cl Avatar asked Dec 13 '16 19:12

badger_cl


1 Answers

It looks like you can access the response from the WKNavigationDelegate method webView:decidePolicyFor:decisionHandler:.

Set some object as the WKWebView's navigationDelegate, and add this method:

- (void)webView:(WKWebView *)webView decidePolicyForNavigationResponse:(WKNavigationResponse *)navigationResponse decisionHandler:(void (^)(WKNavigationResponsePolicy))decisionHandler {
    NSDictionary *headers = ((NSHTTPURLResponse *)navigationResponse.response).allHeaderFields;

    decisionHandler(WKNavigationResponsePolicyAllow);
}
like image 163
NobodyNada Avatar answered Sep 19 '22 09:09

NobodyNada