Skip to content

Commit

Permalink
docs(mobileproxy): explain how to use a proxy in an iOS WKWebView
Browse files Browse the repository at this point in the history
  • Loading branch information
daniellacosse committed Oct 17, 2024
1 parent c7e1aea commit fe635b1
Showing 1 changed file with 22 additions and 1 deletion.
23 changes: 22 additions & 1 deletion x/mobileproxy/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -555,7 +555,28 @@ We are working on instructions on how use the local proxy in a Webview.

On Android, you will likely have to implement [WebViewClient.shouldInterceptRequest](https://developer.android.com/reference/android/webkit/WebViewClient#shouldInterceptRequest(android.webkit.WebView,%20android.webkit.WebResourceRequest)) to fulfill requests using an HTTP client that uses the local proxy.

On iOS, we are still looking for ideas. There's [WKWebViewConfiguration.setURLSchemeHandler](https://developer.apple.com/documentation/webkit/wkwebviewconfiguration/2875766-seturlschemehandler), but the documentation says it can't be used to intercept HTTPS. If you know how to use a proxy with the WKWebView, please let us know!
As of iOS 17, you can add a proxy configuration to a `WKWebView` via its `WKWebsiteDataStore` property.

```swift
let configuration = WKWebViewConfiguration()

let endpoint = NWEndpoint.hostPort(
host: NWEndpoint.Host(proxyHost),
port: NWEndpoint.Port(proxyPort)!
)
let proxyConfig = ProxyConfiguration.init(httpCONNECTProxy: endpoint)

let websiteDataStore = WKWebsiteDataStore.default()
websiteDataStore.proxyConfigurations = [proxyConfig]

// Other webview configuration options... see https://developer.apple.com/documentation/webkit/wkwebviewconfiguration

let webview = WKWebView(
configuration: configuration,
)

// use this webview as you would normally!
```

## Clean up

Expand Down

0 comments on commit fe635b1

Please sign in to comment.