Using Proxies in Swift
Proxies in Swift
Swift is the primary language for developing iOS and macOS applications. Working with proxies in Swift is done through URLSession (a high-level API), the Network framework (low-level), and system settings.
URLSession with Proxies
Configuration via URLSessionConfiguration
import Foundation
let config = URLSessionConfiguration.default
// HTTP proxy configuration
config.connectionProxyDictionary = [
kCFNetworkProxiesHTTPEnable: true,
kCFNetworkProxiesHTTPProxy: "proxy_ip",
kCFNetworkProxiesHTTPPort: 8080,
kCFNetworkProxiesHTTPSEnable: true,
kCFNetworkProxiesHTTPSProxy: "proxy_ip",
kCFNetworkProxiesHTTPSPort: 8080
]
let session = URLSession(configuration: config)
let url = URL(string: "https://httpbin.org/ip")!
let task = session.dataTask(with: url) { data, response, error in
if let data = data, let body = String(data: data, encoding: .utf8) {
print(body)
}
}
task.resume()
SOCKS Proxy
config.connectionProxyDictionary = [
kCFStreamPropertySOCKSProxyHost: "proxy_ip",
kCFStreamPropertySOCKSProxyPort: 1080,
kCFStreamPropertySOCKSVersion: kCFStreamSocketSOCKSVersion5
]
With Authentication
config.connectionProxyDictionary = [
kCFNetworkProxiesHTTPEnable: true,
kCFNetworkProxiesHTTPProxy: "proxy_ip",
kCFNetworkProxiesHTTPPort: 8080,
kCFProxyUsernameKey: "username",
kCFProxyPasswordKey: "password"
]
System Proxy Settings
Automatic Usage
By default, URLSession uses system proxies:
let config = URLSessionConfiguration.default
// Uses system proxies by default
let session = URLSession(configuration: config)
If the user has configured proxies in iOS/macOS system settings, URLSession will automatically use them.
PAC File
config.connectionProxyDictionary = [
kCFNetworkProxiesProxyAutoConfigEnable: true,
kCFNetworkProxiesProxyAutoConfigURLString: "http://example.com/proxy.pac"
]
Handling Proxy Authentication Challenge
When a proxy requires authentication, URLSession invokes its delegate:
class ProxyDelegate: NSObject, URLSessionDelegate {
func urlSession(
_ session: URLSession,
didReceive challenge: URLAuthenticationChallenge,
completionHandler: @escaping (URLSession.AuthChallengeDisposition, URLCredential?) -> Void
) {
if challenge.protectionSpace.authenticationMethod == NSURLAuthenticationMethodHTTPBasic
&& challenge.protectionSpace.isProxy {
let credential = URLCredential(
user: "username",
password: "password",
persistence: .forSession
)
completionHandler(.useCredential, credential)
} else {
completionHandler(.performDefaultHandling, nil)
}
}
}
let delegate = ProxyDelegate()
let session = URLSession(configuration: config, delegate: delegate, delegateQueue: nil)
Network Framework (NWConnection)
For low-level network operations in iOS 12+ / macOS 10.14+:
import Network
let proxyConfig = NWProtocolTLS.Options()
let params = NWParameters.tls
// Proxy configuration via NWEndpoint
let proxyEndpoint = NWEndpoint.hostPort(
host: NWEndpoint.Host("proxy_ip"),
port: NWEndpoint.Port(rawValue: 8080)!
)
// Creating a connection
let connection = NWConnection(
host: "httpbin.org",
port: 443,
using: params
)
connection.stateUpdateHandler = { state in
switch state {
case .ready:
print("Connected")
case .failed(let error):
print("Failed: \(error)")
default:
break
}
}
connection.start(queue: .global())
Alamofire with Proxies
Alamofire is a popular Swift HTTP library:
import Alamofire
let config = URLSessionConfiguration.default
config.connectionProxyDictionary = [
kCFNetworkProxiesHTTPEnable: true,
kCFNetworkProxiesHTTPProxy: "proxy_ip",
kCFNetworkProxiesHTTPPort: 8080
]
let session = Session(configuration: config)
session.request("https://httpbin.org/ip").response { response in
if let data = response.data, let body = String(data: data, encoding: .utf8) {
print(body)
}
}
iOS Specifics
VPN and Proxies in iOS
iOS allows proxy configuration via:
1. System Settings (Settings → Wi-Fi → HTTP Proxy)
2. MDM profiles
3. NEProxySettings (Network Extension framework)
Network Extension
To create a full-fledged proxy application on iOS, use NETransparentProxyProvider:
import NetworkExtension
class ProxyProvider: NETransparentProxyProvider {
override func startProxy(options: [String: Any]?, completionHandler: @escaping (Error?) -> Void) {
// Proxy configuration
completionHandler(nil)
}
override func handleNewFlow(_ flow: NEAppProxyFlow) -> Bool {
// Handling a new connection
return true
}
}
This requires an entitlement from Apple and is suitable for Enterprise/MDM solutions.
async/await (Swift 5.5+)
func fetchViaProxy() async throws -> String {
let config = URLSessionConfiguration.default
config.connectionProxyDictionary = [
kCFNetworkProxiesHTTPEnable: true,
kCFNetworkProxiesHTTPProxy: "proxy_ip",
kCFNetworkProxiesHTTPPort: 8080
]
let session = URLSession(configuration: config)
let url = URL(string: "https://httpbin.org/ip")!
let (data, _) = try await session.data(from: url)
return String(data: data, encoding: .utf8) ?? ""
}
// Usage
Task {
let ip = try await fetchViaProxy()
print(ip)
}
Conclusion
Swift provides flexible tools for working with proxies: URLSession for high-level tasks, the Network framework for low-level control, and Network Extension for creating proxy applications. For most tasks, URLSession with connectionProxyDictionary is the optimal choice.