I'm trying to load a self signed website. I know I can use NSURLConnection to give credential by using this code
UIWebView to view self signed websites (No private api, not NSURLConnection) - is it possible?
- (void)connection:(NSURLConnection *)connection willSendRequestForAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge
{
[challenge.sender useCredential: [NSURLCredential credentialForTrust:challenge.protectionSpace.serverTrust] forAuthenticationChallenge: challenge];
}
and load the request in
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
[self.webviewVc.webview loadRequest:_FailedRequest];
}
But when I try to use NSURLSession to do the similar thing, I'm always getting the error
NSURLConnection/CFURLConnection HTTP load failed (kCFStreamErrorDomainSSL, -9813)
My code looks like this
- (BOOL)loadWithRequest:(NSURLRequest *)request
{
if (!_Authenticated ) {
_FailedRequest = request;
NSURLSessionConfiguration* defaultConfigObject = [NSURLSessionConfiguration defaultSessionConfiguration];
NSURLSession *session = [NSURLSession sessionWithConfiguration:defaultConfigObject delegate:self delegateQueue:nil];
NSURLSessionDataTask * task = [session dataTaskWithRequest:_FailedRequest];
[task resume];
return NO;
}
_Authenticated = NO;
return YES;
}
-(void)URLSession:(NSURLSession *)session didReceiveChallenge:(NSURLAuthenticationChallenge *)challenge completionHandler:(void (^)(NSURLSessionAuthChallengeDisposition, NSURLCredential * _Nullable))completionHandler
{
completionHandler(NSURLSessionAuthChallengeUseCredential, [NSURLCredential credentialForTrust:challenge.protectionSpace.serverTrust]);
}
-(void)URLSession:(NSURLSession *)session dataTask:(NSURLSessionDataTask *)dataTask didReceiveData:(NSData *)data
{
[self.webviewVc.webview loadRequest:_FailedRequest];
}
Any idea? Thanks