How to add custom HTTP Header on WKWebView

Hi there.


I would like to create a custom

WKWebView
class, because I want to set a custom
HTTP
header. But I get this error:
Overriding method with selector 'loadRequest:' has incompatible type '(NSURLRequest) -> WKNavigation?'


My code is the following code.

import WebKit
class CustomWkWebView: WKWebView {

    override func loadRequest(request: NSURLRequest) -> WKNavigation? {
        var mutable_request = request.mutableCopy() as! NSMutableURLRequest
        mutable_request.setValue("SHOTWORKS_IOS_APLICATION", forHTTPHeaderField: "App-MyParameter")
        return super.loadRequest(mutable_request.copy() as! NSURLRequest)
     
    }
}


I have only written it by suggestion.

I don't know why i couldn't override.

What should I do?

Following capture is this error.

Replies

I am also having the same issue. Can any one help us out ?

We have same probrem, too.

When using UIWebView, We add custom headers and custom error handling with URLProtocol.

But, WKWebView can not support URLProtocol...


How to add custom HTTP headers to ALL REQUEST (contains AJAX Request etc.) in WKWebView?

Hi, I have same issue. Btw do that problem already fixed?

If yes, could you share us, how to fix it?
Thanks..

>But, WKWebView can not support URLProtocol...


It can.....

https://developer.apple.com/documentation/webkit/wkwebview/1414954-loadrequest?language=objc