I've configured a proxy for the NSURLSessionConfiguration object in my app, the proxy use the "Digest" authentication scheme and this occurs only within HTTPS requests.
Normal in Simulator, but popup a dialog stating the proxy authentication is required and gives the user the choice to do this "later" or directly go to the system settings in iPhone real device.
In addition, the dialog pops up even before the delegate method "URLSession:task:didReceiveChallenge:completionHandler:", the application does not get an opportunity to provide credentials before iOS displays it, but it's very strange that click on "later" will execute the delegate.
demo code
NSURLSessionConfiguration *config = [NSURLSessionConfiguration defaultSessionConfiguration];
config.requestCachePolicy = NSURLRequestReloadIgnoringLocalCacheData;
config.connectionProxyDictionary = @{
@"HTTPEnable" : @(1),
(NSString *)kCFStreamPropertyHTTPProxyHost : @"proxy.xxxx.com",
(NSString *)kCFStreamPropertyHTTPProxyPort : @(1234),
@"HTTPSEnable": @(1),
(NSString *)kCFStreamPropertyHTTPSProxyHost : @"proxy.xxxx.com",
(NSString *)kCFStreamPropertyHTTPSProxyPort : @(1234)
};
self.session = [NSURLSession sessionWithConfiguration:config delegate:self delegateQueue:[NSOperationQueue mainQueue]];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:RequestUrl] cachePolicy:NSURLRequestReloadIgnoringCacheData timeoutInterval:60.0];
NSURLSessionTask *task = [self.session dataTaskWithRequest:request completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
// NSLog(@"resultData:%@ error: %@",data, error.description);
}];
[task resume];
- (void)URLSession:(NSURLSession *)session task:(NSURLSessionTask *)task
didReceiveChallenge:(NSURLAuthenticationChallenge *)challenge
completionHandler:(void (^)(NSURLSessionAuthChallengeDisposition disposition, NSURLCredential * _Nullable credential))completionHandler
{
// NSLog(@"task:%@",challenge.protectionSpace.authenticationMethod);
if (challenge.protectionSpace.authenticationMethod == NSURLAuthenticationMethodHTTPDigest) {
NSURLCredential *cren = [NSURLCredential credentialWithUser:FLOW_UNICOM_KEY password:FLOW_UNICOM_SECRET persistence:NSURLCredentialPersistenceNone];
if (completionHandler) {
completionHandler(NSURLSessionAuthChallengeUseCredential,cren);
}
}
if (completionHandler) {
//completionHandler(NSURLSessionAuthChallengePerformDefaultHandling,nil);
}
}