How to programmatically add a proxy to an NSURLSession

前端 未结 6 1241
醉酒成梦
醉酒成梦 2020-12-01 05:02

Looking over the documentation of NSURLSession and NSURLSessionConfiguration, I was under the impression I should configure it with a dictionary li

6条回答
  •  半阙折子戏
    2020-12-01 05:18

    Based on all previous responses, this works for Swift 4, both HTTP and HTTPS:

    let proxyHost = "127.0.0.1"
    let proxyPort = 8888
    let configuration = URLSessionConfiguration.default
    configuration.connectionProxyDictionary = [
        kCFNetworkProxiesHTTPEnable: true,
        kCFNetworkProxiesHTTPProxy: proxyHost,
        kCFNetworkProxiesHTTPPort: proxyPort,
        kCFNetworkProxiesHTTPSEnable: true,
        kCFNetworkProxiesHTTPSProxy: proxyHost,
        kCFNetworkProxiesHTTPSPort: proxyPort
    ]
    

    proxyHost should be a hostname and not contain any URL scheme.

提交回复
热议问题