Swift 4/5 设置向 WKWebview 添加代理

use*_*482 5 ios nsurlsessionconfiguration swift wkwebview wkwebviewconfiguration

我一直在寻找一种方法来为 WKWebview 请求设置代理,但没有运气。

另一方面,我已经能够将代理设置为 http 请求

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
}
Run Code Online (Sandbox Code Playgroud)

但是我对你们的问题是如何将代理设置为 WKWebView?

我会非常感谢你的帮助

roo*_*oop 4

WKWebView 无法使用 HTTP 代理 - NSURLProtocol 注册技巧在使用 WKWebView 时不起作用(它仅适用于 UIWebView)。

使用 WKURLSchemeHandler 可能可以实现类似的技巧,但它不允许注册由 WKWebView 本机处理的方案,因此实际上不可能。

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