5

I have looking for a way to set a proxy to WKWebview request with no luck.

By another hand I have been able to set proxy to http request

func createURLSessionConfiguration(_ host:String,_  port:String) -> URLSessionConfiguration {

    let config = URLSessionConfiguration.default
    config.requestCachePolicy = URLRequest.CachePolicy.reloadIgnoringLocalCacheData
    config.connectionProxyDictionary = [AnyHashable: Any]()
    config.connectionProxyDictionary?[kCFNetworkProxiesHTTPEnable as String] = 1
    config.connectionProxyDictionary?[kCFNetworkProxiesHTTPSProxy as String] = host
    config.connectionProxyDictionary?[kCFNetworkProxiesHTTPSPort as String] = port

    return config
}

But my question to you guys is how can set the proxy to the WKWebView?

I'll really appreciate your help

user2924482
  • 6,844
  • 16
  • 65
  • 133

3 Answers3

4

HTTP proxying is not possible with WKWebView -- the NSURLProtocol registering trick doesn't work when using WKWebView (it works only with UIWebView).

A similar trick might have been possible using WKURLSchemeHandler, but it doesn't allow registering schemes that are natively handled by WKWebView, so it isn't really possible.

Source: https://forums.developer.apple.com/thread/110312#337642

roop
  • 1,221
  • 7
  • 8
0

Web views are used for entering very sensitive information (e.g. credit card numbers, passwords) that you don't want the application to access directly.

Therefore, WKWebview explicitly blocks all attempts to hijack that information.

For example, in delegate methods you can see that a request is happening and see its URL, but you cannot access request body.

For the same reason you are not allowed to tamper with the proxy.

If you could actually do that, it would make web views very dangerous to use.

Sulthan
  • 118,286
  • 20
  • 194
  • 245
0

We have limited access to WKWebView with minimum delegate we can achieve it NSURLProtocol If your proxy server supports tunnelling(VPN) then you can use NetworkExtension.framework see the content here hopefully it will help out you. source is here