首页 > 解决方案 > Swift 4/5 设置将代理添加到 WKWebview

问题描述

我一直在寻找一种方法来为 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
}

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

我会非常感谢你的帮助

标签: iosswiftwkwebviewnsurlsessionconfigurationwkwebviewconfiguration

解决方案


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

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

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


推荐阅读