📁
til
  • Today I Learned
  • Bookmarks
    • Bookmarks
  • Centos
    • How to limit bandwidth on CentOS
  • CI/CD
    • Add a mac as a GitLab runner
    • Setup CI pipeline for iOS projects on gitlab.com
  • cocoapods
    • private-spec-repo
    • CocoaPods save project.pbxproj file in XML plist format on Xcode 7.3.1
  • git
    • Git keeps asking password after El Capitan Upgrade
    • change-case-sensitivity-of-filename
    • interactive-rebase
    • Fork a repo
  • hacking
    • Decompile Android apk
  • homebrew
    • Bash Completion
  • ios
    • ats
    • Failed to open Xcode (LSOpenURLsWithRole() failed with error -10699)
    • Redirect to Settings Page
    • Retrieve expiry date of Provisioning Profile Certificate from .ipa
    • WKWebView set custom HTTP headers
    • IAP applicationUsername is nil
  • Jenkins
    • Create stage dynamically in declarative pipeline
  • mac
    • Catalina failed to sync iPhone with Finder
  • networking
    • shadowsocks vs. VPN
  • Objective-C
    • Keep subview in Scroll View always on screen
    • Custom View using xib
    • Scroll up TextField when keyboard shows
    • autolayout-hugging-vs-resistance
  • Regex
    • regex-chinese-char
  • SQL
    • update-json-value-in-postgresql
    • select-random-row-in-sql
  • SSH
    • verify-ssh-passphrase
  • SVN
    • Svn Checkout Directories only
  • swift
    • equatable
  • unix
    • Create and Grant Sudo Privileges to user
    • Create and Change Current Directory
    • Show ASCII Text welcome message when login with SSH
  • vim
    • Vim with Multiple Files
Powered by GitBook
On this page
  • Problem
  • Solution
  • References

Was this helpful?

  1. ios

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

PreviousRetrieve expiry date of Provisioning Profile Certificate from .ipaNextIAP applicationUsername is nil

Last updated 5 years ago

Was this helpful?

WKWebView ignores NSURLRequest body | Apple Developer Forums
解决WKWebView加载POST请求无法发送参数问题
Bug 167131 - It's not possible to perform a POST request with HTTPBody content in a WKWebView
Can't set headers on my WKWebView POST request | StackOverflow