Configure NWConnection SOCKS5 Proxying on iOS 17+
Attach an iOS 17 SOCKSv5 ProxyConfiguration to NWParameters.PrivacyContext before constructing an NWConnection.
On iOS 17 and later, Network.framework can apply a SOCKSv5 proxy through a privacy context. Attach that context to the parameters before constructing the connection:
import Network
@available(iOS 17.0, *)
func makeConnection() -> NWConnection {
let parameters = NWParameters.tls
let context = NWParameters.PrivacyContext(description: "Service route")
let endpoint = NWEndpoint.hostPort(
host: "proxy.example", port: 1080)
context.proxyConfigurations = [
ProxyConfiguration(socksv5Proxy: endpoint)
]
parameters.setPrivacyContext(context)
return NWConnection(
host: "service.example", port: 443, using: parameters)
}
The function returns an unstarted TLS connection. Install the application’s state handling, start it on the intended queue, then exchange the application protocol through its normal send and receive path.
proxy.example:1080 is the SOCKS listener. For permitted public-service connections, the SOCKS5 proxy list supplies host and port candidates. service.example:443 remains the destination. The privacy context applies its proxy configurations to connections associated with that context; creating an unrelated connection with other parameters does not inherit this setup.
This is Network.framework’s connection interface. A URLSession proxy override or a WKWebView data-store setting configures those separate consumers. For a WebSocket built on NWConnection, the application must also supply the appropriate WebSocket protocol options.