6

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

Community
  • 1
  • 1
riowww
  • 123
  • 1
  • 10
  • Not sure about this exact error, but some network-related `NSError`s has recovery attempter, that may be used to recover from error and continue loading. – Borys Verebskyi Feb 15 '16 at 21:25
  • is `loadWithRequest` a delegate method? I thought the one you'd want is `loadRequest`? – Tri Nguyen May 30 '16 at 23:16

0 Answers0