展望了本文档NSURLSession和NSURLSessionConfiguration,我的印象是我应该像下面这样的字典进行配置下:
// 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使用此配置创建的请求将直接连接.
如何为使用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)
但我不知道如何配置它以使用代理。