问题描述
是否可以将WKWebView配置为通过HTTP代理服务器?我知道直接使用NSURLSession可以实现,但是我想对其进行配置,以便通过WKWebView浏览器的所有请求都通过代理.
Is there a way to configure WKWebView to go through a HTTP Proxy server? I know this is possible with NSURLSession directly, but I want to configure it so that all requests through the WKWebView browser go through a proxy.
推荐答案
NSURLSessionConfiguration 具有直接的接口( connectionProxyDictionary ),因此可以轻松完成
NSURLSessionConfiguration has a direct interface(connectionProxyDictionary) to do that , so it can be done easily
但是,WkWebView没有这样的直接接口,它请求,渲染内容超出进程范围,因此您可以想象WkWebview实例是从应用程序的其余部分沙盒化的
But, WkWebView does not have any direct interface like that,it requests ,renders the content out-of-process and so you can imagine like WkWebview instance is sandboxed from the rest of your app
WkWeView甚至忽略了NSURLSession&的cookie(NSHTTPCookieStorage),缓存(NSURLCache)和凭据(NSCredentialStorage).同样,NSURLConnection这些网络类也无法访问WkWebView实例的cookie,缓存和凭据.
WkWeView even ignores the cookies(NSHTTPCookieStorage), caches(NSURLCache) and Credentials(NSCredentialStorage) of NSURLSession & NSURLConnection likewise these network classes can't access cookies,caches and credentials of WkWebView instance.
UIWebView不会在进程外执行此操作,因此您可以使用NSURLProtocol,如 CustomHTTPProtocol 示例以代理服务器.
The UIWebView does not do it out-of-process and so you can use NSURLProtocol as demonstrated in CustomHTTPProtocol sample to proxy to a server.
从iOS 10开始,WkWebView仍不支持NSURLProtocol,因此您无法使用WkWebView进行代理.
As of iOS 10, still there is no support for NSURLProtocol in WkWebView so you can't proxy using WkWebView.
如果您的代理服务器支持隧道(VPN),则可以使用 NetworkExtension.framework 及其类(NEVPNManager,NEVPNProtocol,NEVPNConnection等)来引导您应用的所有网络流量通过代理服务器.
If your proxy server supports tunnelling(VPN) then you can use NetworkExtension.framework and its classes(NEVPNManager, NEVPNProtocol, NEVPNConnection etc..) to direct all the network traffic of your app through your proxy server.
这篇关于WKWebView的HTTP代理支持的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!