Post

Replies

Boosts

Views

Activity

Reply to What is the recommended way to programmatically apply proxy to WKWebView
@yueqiz Hello! I have simple solution for your needs. There is "new" Apple's API which provides ability to setup proxy in WKWebView native since iOS 17.0, macOS 14.0 Create Network.NWEndpoint with proxy host and port Create Network.ProxyConfiguration with created endpoint Set created proxy configuration to WKWebsiteDataStore (default or other) via .proxyConfigurations Create new WKWebViewConfiguration Set WKWebsiteDataStore from step 3 to created WKWebViewConfiguration via .websiteDataStore Create WKWebView with created configuration Process authorization via WKNavigationDelegate.webView(_:didReceive:completionHandler:) if required. Note: If set proxy configuration to WKWebsiteDataStore.default(), all WKWebView with default configuration will use this proxy. If use not default WKWebsiteDataStore for web view, website data should not be synchronized between default store and specific one. Network.ProxyConfiguration supports proxy with authorization by IP whitelist and SOCKS5. Example: import Foundation import Network import WebKit ... let endpoint = NWEndpoint(host: <PROXY_HOST>, port: <PROXY_POST>) let proxyConfiguration = ProxyConfiguration(httpCONNECTProxy: endpoint) let websiteDataStore = WKWebsiteDataStore.default() websiteDataStore.proxyConfigurations = [proxyConfiguration] let webViewConfiguration = WKWebViewConfiguration() webViewConfiguration.websiteDataStore = websiteDataStore let webView = WKWebView(frame: .zero, configuration: webViewConfiguration) ... // Optional: If proxy requires authorization @MainActor optional func webView( _ webView: WKWebView, respondTo challenge: URLAuthenticationChallenge ) async -> (URLSession.AuthChallengeDisposition, URLCredential?) let proxyCredential = URLCredential(username: "PROXY_USERNAME", password: "PROXY_PASS") return (.useCredential, proxyCredential) }
Topic: Safari & Web SubTopic: General Tags:
2w