How to add HTTP headers in request globally for iOS in swift
Solution 1:
AFAIK sadly you cannot do this with WKWebView
.
It most certainly does not work in webView:decidePolicyForNavigationAction:decisionHandler:
because the navigationAction.request
is read-only and a non-mutable NSURLRequest
instance that you cannot change.
If I understand correctly, WKWebView
runs sandboxed in a separate content and network process and, at least on iOS, there is no way to intercept or change it's network requests.
You can do this if you step back to UIWebView
.
Solution 2:
There are many different ways to do that, I found that the easiest solution was to subclass WKWebView and override the loadRequest method. Something like this:
class CustomWebView: WKWebView {
override func load(_ request: URLRequest) -> WKNavigation? {
guard let mutableRequest: NSMutableURLRequest = request as? NSMutableURLRequest else {
return super.load(request)
}
mutableRequest.setValue("custom value", forHTTPHeaderField: "custom field")
return super.load(mutableRequest as URLRequest)
}
}
Then simply use the CustomWebView class as if it was a WKWebView.
EDIT NOTE: This will only work on the first request as pointed out by @Stefan Arentz.
NOTE: Some fields cannot be overridden and will not be changed. I haven't done a thorough testing but I know that the User-Agent
field cannot be overridden unless you do a specific hack (check here for an answer to that)
Solution 3:
I have modified Au Ris answer to use NavigationAction
instead of NavigationResponse
, as jonny suggested. Also, this fixes situations where the same url is called subsequently and you don't have to keep track of the current url anymore. This only works for GET requests but can surely be adapted for other request types if neccessary.
import UIKit
import WebKit
class ViewController: UIViewController, WKNavigationDelegate {
var webView: WKWebView?
override func viewDidLoad() {
super.viewDidLoad()
webView = WKWebView(frame: CGRect.zero)
webView!.navigationDelegate = self
view.addSubview(webView!)
// [...] set constraints and stuff
// Load first request with initial url
loadWebPage(url: "https://my.url")
}
func loadWebPage(url: URL) {
var customRequest = URLRequest(url: url)
customRequest.setValue("true", forHTTPHeaderField: "x-custom-header")
webView!.load(customRequest)
}
func webView(_ webView: WKWebView, decidePolicyFor navigationAction: WKNavigationAction, decisionHandler: @escaping
(WKNavigationActionPolicy) -> Void) {
if navigationAction.request.httpMethod != "GET" || navigationAction.request.value(forHTTPHeaderField: "x-custom-header") != nil {
// not a GET or already a custom request - continue
decisionHandler(.allow)
return
}
decisionHandler(.cancel)
loadWebPage(url: navigationAction.request.url!)
}
}
Solution 4:
With some limitations, but you can do it. Intercept the response in the delegate function webView:decidePolicyFornavigationResponse:decisionHandler:
, if the url changes cancel it by passing decisionHandler(.cancel)
and reload the webview with newURLRequest
which sets the custom headers and the intercepted url. In this way each time a url changes (e.g. users tap on links) you cancel that request and create a new one with custom headers.
import UIKit
import WebKit
class ViewController: UIViewController, WKNavigationDelegate {
var webView: WKWebView?
var loadUrl = URL(string: "https://www.google.com/")!
override func viewDidLoad() {
super.viewDidLoad()
webView = WKWebView(frame: CGRect.zero)
webView!.navigationDelegate = self
view.addSubview(webView!)
webView!.translatesAutoresizingMaskIntoConstraints = false
webView!.leadingAnchor.constraint(equalTo: view.leadingAnchor, constant: 0).isActive = true
webView!.rightAnchor.constraint(equalTo: view.rightAnchor, constant: 0).isActive = true
webView!.topAnchor.constraint(equalTo: view.topAnchor, constant: 0).isActive = true
webView!.bottomAnchor.constraint(equalTo: view.bottomAnchor, constant: 0).isActive = true
// Load first request with initial url
loadWebPage(url: loadUrl)
}
func loadWebPage(url: URL) {
var customRequest = URLRequest(url: url)
customRequest.setValue("some value", forHTTPHeaderField: "custom header key")
webView!.load(customRequest)
}
// MARK: - WKNavigationDelegate
func webView(_ webView: WKWebView, decidePolicyFor navigationResponse: WKNavigationResponse, decisionHandler: @escaping (WKNavigationResponsePolicy) -> Void) {
guard let url = (navigationResponse.response as! HTTPURLResponse).url else {
decisionHandler(.cancel)
return
}
// If url changes, cancel current request which has no custom headers appended and load a new request with that url with custom headers
if url != loadUrl {
loadUrl = url
decisionHandler(.cancel)
loadWebPage(url: url)
} else {
decisionHandler(.allow)
}
}
}
Solution 5:
Here's how you do it: The strategy is to have your WKNavigationDelegate cancel the request, modify a mutable copy of it and re-initiate it. An if-else is used to allow the request to proceed if it already has the desired header; otherwise you will end up in an endless load / decidePolicy loop.
Not sure what's up, but weird things happen if you set the header on every request, so for best results only set the header on requests to the domain(s) you care about.
The example here sets a header field for requests to header.domain.com, and allows all other requests without the header:
- (void)webView:(WKWebView *)webView decidePolicyForNavigationAction:(WKNavigationAction *)navigationAction decisionHandler:(void (^)(WKNavigationActionPolicy))decisionHandler {
NSURL * actionURL = navigationAction.request.URL;
if ([actionURL.host isEqualToString:@"header.domain.com"]) {
NSString * headerField = @"x-header-field";
NSString * headerValue = @"value";
if ([[navigationAction.request valueForHTTPHeaderField:headerField] isEqualToString:headerValue]) {
decisionHandler(WKNavigationActionPolicyAllow);
} else {
NSMutableURLRequest * newRequest = [navigationAction.request mutableCopy];
[newRequest setValue:headerValue forHTTPHeaderField:headerField];
decisionHandler(WKNavigationActionPolicyCancel);
[webView loadRequest:newRequest];
}
} else {
decisionHandler(WKNavigationActionPolicyAllow);
}
}