iosswiftnetwork-programmingproxy

Programmatically configure proxy settings in iOS with the Network library


How can I set the proxy settings for a connection established with Network (and not using URLSession) ?

As described in this answer, one can do so with URLSession by updating the configuration:

configuration.connectionProxyDictionary = [
    kCFNetworkProxiesHTTPEnable as String: 1,
    kCFNetworkProxiesHTTPProxy as String: ip,
    kCFNetworkProxiesHTTPPort as String: port,
    "HTTPSEnable": 1,
    "HTTPSProxy": ip,
    "HTTPSPort": port,
]

I would like to do something similar using the Network library.

I am currently creating my connection as:

NWConnection(host: host, port: port, using: .init())

but I don't know how to configure it to use a Proxy.


Solution

  • There is no in-built proxy support in the Network framework. It provides access to raw TCP streams and UDP packets.

    If you are using the Network framework to work with the HTTP protocol then you will need to write your own proxy support (and why would you do this?; just use URLSession).

    If you want to tunnel an arbitrary TCP stream through a web proxy using CONNECT then you will need to implement this yourself.

    If you are using some other TCP or UDP protocol then you could use a SOCKS proxy, but again you will need to implement this.