相关疑难解决方法(0)

如何以编程方式将代理添加到NSURLSession

展望了本文档NSURLSessionNSURLSessionConfiguration,我的印象是我应该像下面这样的字典进行配置下:

    // Create a dictionary to describe the proxy 
    NSDictionary *proxyDict = @{
        (NSString *)kCFProxyHostNameKey   : @"myProxyHost.com",
        (NSString *)kCFProxyPortNumberKey : @"12345",
        (NSString *)kCFProxyTypeKey       : (NSString*)kCFProxyTypeHTTP
    };

    // Create a configuration that uses the dictionary
    NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration defaultSessionConfiguration];
    [configuration setConnectionProxyDictionary:proxyDict];
Run Code Online (Sandbox Code Playgroud)

但是,NSURLSession使用此配置创建的请求将直接连接.

proxy ios nsurlsession nsurlsessionconfiguration

12
推荐指数
3
解决办法
1万
查看次数

使用网络库以编程方式在 iOS 中配置代理设置

如何为使用Network(而不是使用URLSession)建立的连接设置代理设置?

正如此答案中所述,可以URLSession通过更新配置来做到这一点:

configuration.connectionProxyDictionary = [
    kCFNetworkProxiesHTTPEnable as String: 1,
    kCFNetworkProxiesHTTPProxy as String: ip,
    kCFNetworkProxiesHTTPPort as String: port,
    "HTTPSEnable": 1,
    "HTTPSProxy": ip,
    "HTTPSPort": port,
]
Run Code Online (Sandbox Code Playgroud)

我想使用该Network库做类似的事情。

我目前正在创建连接:

NWConnection(host: host, port: port, using: .init())
Run Code Online (Sandbox Code Playgroud)

但我不知道如何配置它以使用代理。

proxy networking ios swift

5
推荐指数
1
解决办法
754
查看次数