When connecting via HTTPS to the server, I implement a method NSURLSessionDelegate URLSession:didReceiveChallenge:completionHandler:for implementing some user-defined functions.
The problem is that this delegate method is called only on the first request (subsequent requests do not call this method). My custom functionality requires that the delegate method be called for the request each .
Here is an example:
- (IBAction)reload:(id)sender {
NSURLSession *session = [NSURLSession sessionWithConfiguration:[NSURLSessionConfiguration ephemeralSessionConfiguration] delegate:self delegateQueue:nil];
NSURL *URL = [NSURL URLWithString:@"https://www.example.com"];
NSMutableURLRequest *URLRequest = [NSMutableURLRequest requestWithURL:URL];
[[session dataTaskWithRequest:URLRequest
completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
}] resume];
}
#pragma NSURLSessionDelegate
- (void)URLSession:(NSURLSession *)session didReceiveChallenge:(NSURLAuthenticationChallenge *)challenge completionHandler:(void (^)(NSURLSessionAuthChallengeDisposition disposition, NSURLCredential *credential))completionHandler
{
completionHandler(NSURLSessionAuthChallengeUseCredential, [NSURLCredential credentialForTrust:challenge.protectionSpace.serverTrust]);
}
Since I need a URLCredential for the session or for each task, I checked NSURLCredentialwhich I pass on completionHandler, and found that it has persistenceof NSURLCredentialPersistenceForSession(which is immutable) which seems to be correct.
[NSURLCredentialStorage allCredentials], , .
, URL- HTTPS , , .
, ?
NSURLSessionTaskDelegate URLSession:task:didReceiveChallenge:completionHandler: .
- (void)URLSession:(NSURLSession *)session task:(NSURLSessionTask *)task didReceiveChallenge:(NSURLAuthenticationChallenge *)challenge completionHandler:(void (^)(NSURLSessionAuthChallengeDisposition disposition, NSURLCredential *credential))completionHandler
{
completionHandler(NSURLSessionAuthChallengeUseCredential, [NSURLCredential credentialForTrust:challenge.protectionSpace.serverTrust]);
}
EDIT
, , , Apple: 19072802