Reputation: 145
With the code below I create a webview navigating to a URL. It works perfectly. But I need this navigation to be through an HTTP proxy. How do I put proxy in a webview?
import UIKit
import WebKit
class ViewController: UIViewController, WKNavigationDelegate {
var webView: WKWebView!
override func viewDidLoad() {
super.viewDidLoad()
webView = WKWebView()
webView.navigationDelegate = self
view = webView
let url = URL(string: "https://www.iphub.info")!
webView.load(URLRequest(url: url))
webView.allowsBackForwardNavigationGestures = true
}
}
Upvotes: 0
Views: 1072
Reputation: 476
Unfortunately, there's no simple way to achieve that.
You may intercept deprecated UIWebView
with custom NSURLProtocol
subclass, which utilizes NSURLSession
inside. You'd easily set HTTP proxy configuration to the connectionProxyDictionary
property of the NSURLSessionConfigurtion
. Here's the example. That would work, but you may see slightly worse performance than with WKWebView.
Alternatively, you may consider using NetworkExtension.framework
, if the proxy server supports tunneling. Here's a sample project which utilizes Network Extension.
Upvotes: 1