13

How to configure proxy settings in iOS programmatically in NSURLSession ? I followed this link.

But it seems kCFStreamPropertyHTTPProxyHost, kCFStreamPropertyHTTPProxyPort are deprecated from iOS 9. SO what's the way to create proxy programmatically?

Cœur
  • 37,241
  • 25
  • 195
  • 267
sasi kumar
  • 723
  • 5
  • 14

1 Answers1

1

There are 2 threads in Apple Developer Forums regarding this matter.

The oldest one, indicates that we should use kCFNetworkProxiesHTTPEnable, kCFNetworkProxiesHTTPProxy and kCFNetworkProxiesHTTPPort keys instead of the deprecated ones kCFStreamPropertyHTTPProxyHost and kCFStreamPropertyHTTPProxyPort. But this only fixes the warnings regarding HTTP proxy settings.

Referring to the HTTPS proxy keys, there is the following statement:

Please file a bug with the details and continue using the deprecated keys for now.

and at the bottom of the thread, this:

AFAIK you won't need the matching ‘HTTPS’ constants—which are OS X only—because iOS infers one from the other.

But apparently this doesn't work.

In the more recent thread, there is a response that instead of these keys, uses the constant strings "HTTPSEnable", "HTTPSProxy" and "HTTPSPort" for the HTTPS case.

So, either the bug is that the HTTPS proxy keys are not available for the iOS platform or that the new keys are not working as expected for the HTTPS proxies.

As far as I can tell, you can avoid the warnings for sure if you use HTTP proxy, like that:

NSURLSessionConfiguration *configuration = NSURLSessionConfiguration.defaultSessionConfiguration;
NSDictionary * connectionProxyDictionary = @{
    (NSString *)kCFNetworkProxiesHTTPEnable: [NSNumber numberWithInt: 1],
    (NSString *)kCFNetworkProxiesHTTPProxy: @"127.0.0.1",
    (NSString *)kCFNetworkProxiesHTTPPort: [NSNumber numberWithInt: 8888]
};
[configuration setConnectionProxyDictionary:connectionProxyDictionary];
NSURLSession *session = [NSURLSession sessionWithConfiguration:configuration];
let configuration = URLSessionConfiguration.default
configuration.connectionProxyDictionary = [
    kCFNetworkProxiesHTTPEnable: true,
    kCFNetworkProxiesHTTPProxy: "127.0.0.1",
    kCFNetworkProxiesHTTPPort: 8888
]
let session = URLSession(configuration: configuration)

If you use HTTPS proxy you probably have to stick with the deprecated keys:

NSURLSessionConfiguration *configuration = NSURLSessionConfiguration.defaultSessionConfiguration;
NSDictionary * connectionProxyDictionary = @{
    @"HTTPSEnable": [NSNumber numberWithInt: 1],
    (NSString *)kCFStreamPropertyHTTPSProxyHost: @"127.0.0.1",
    (NSString *)kCFStreamPropertyHTTPSProxyPort: [NSNumber numberWithInt: 8888]
};
[configuration setConnectionProxyDictionary:connectionProxyDictionary];
NSURLSession *session = [NSURLSession sessionWithConfiguration:configuration];
let configuration = URLSessionConfiguration.default
configuration.connectionProxyDictionary = [
    "HTTPSEnable": true,
    kCFStreamPropertyHTTPSProxyHost: "127.0.0.1",
    kCFStreamPropertyHTTPSProxyPort: 8888
]
let session = URLSession(configuration: configuration)

or use their respective String constants:

NSURLSessionConfiguration *configuration = NSURLSessionConfiguration.defaultSessionConfiguration;
NSDictionary * connectionProxyDictionary = @{
    @"HTTPSEnable": [NSNumber numberWithInt: 1],
    @"HTTPSProxy": @"127.0.0.1",
    @"HTTPSPort": [NSNumber numberWithInt: 8888]
};
[configuration setConnectionProxyDictionary:connectionProxyDictionary];
NSURLSession *session = [NSURLSession sessionWithConfiguration:configuration];
let configuration = URLSessionConfiguration.default
configuration.connectionProxyDictionary = [
    "HTTPSEnable": true,
    "HTTPSProxy": "127.0.0.1",
    "HTTPSPort": 8888
]
let session = URLSession(configuration: configuration)

Both of the last 2 configurations would work.

gcharita
  • 7,729
  • 3
  • 20
  • 37