WKWebView set custom HTTP headers

To set HTTP headers before the webview navigate to a new page, just cancel the existing request and load a new request:

- (void)webView:(WKWebView *)webView decidePolicyForNavigationAction:(WKNavigationAction *)navigationAction decisionHandler:(void (^)(WKNavigationActionPolicy))decisionHandler
{
    NSString *headerField = @"Authorization";
    NSString *headerValue = [NSString stringWithFormat:@"Bearer %@", accessToken];

    if([[navigationAction.request valueForHTTPHeaderField:headerField] isEqualToString:headerValue]) {
        decisionHandler(WKNavigationActionPolicyAllow);
    } else {
        NSMutableURLRequest *newRequest = [[NSMutableURLRequest alloc] initWithURL:navigationAction.request.URL];
        [newRequest setValue:headerValue forHTTPHeaderField:headerField];

        decisionHandler(WKNavigationActionPolicyCancel);
        [self.webView loadRequest:newRequest];
    }
}

Problem

This method is not working for POST request, as navigationAction.request.HTTPBody is always nil

Solution

  1. Use Javascript to send POST request with custom HTTP headers

  2. Call Javascript function from Native call - (void)evaluateJavaScript:(NSString *)javaScriptString completionHandler:(void (^ _Nullable)(_Nullable id, NSError * _Nullable error))completionHandler;

References

Last updated