2

I want to post something to a web service which has SSL problem. I used following methods:

NSURLConnection * urlConnection = [[NSURLConnection alloc] initWithRequest:request delegate:self startImmediately:YES];

It should immediately start sending data to which has been set in the request; But Service has security problem and it doesn't work properly. However I want to send data and want to ignore security problem; So I used following methods of NSURLConnectionDelegate:

- (BOOL)connection:(NSURLConnection *)connection canAuthenticateAgainstProtectionSpace:(NSURLProtectionSpace *)protectionSpace {
  return [protectionSpace.authenticationMethod isEqualToString:NSURLAuthenticationMethodServerTrust];
}

- (void)connection:(NSURLConnection *)connection didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge {
  [challenge.sender useCredential:[NSURLCredential credentialForTrust:challenge.protectionSpace.serverTrust] forAuthenticationChallenge:challenge];

  [challenge.sender continueWithoutCredentialForAuthenticationChallenge:challenge];
}

But they has been deprecated. How can I handle security problem and tell to pass data to web service without considering it?

aakpro
  • 1,538
  • 2
  • 19
  • 53

1 Answers1

1

you should use willSendRequestForAuthenticationChallenge like this.

- (void)connection:(NSURLConnection *)connection willSendRequestForAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge
{
    self.challenge = challenge;
    [self askUserAcceptSSLError];
}

- (void)askUserAcceptSSLError
{
    // Ask user like UIAlertView or so.
    // Put these responses in UIAlertView delegate ...

    // If User accepts (or force this if you want ignore SSL certificate errors):
    [[self.challenge sender] 
        useCredential:[NSURLCredential credentialForTrust:self.challenge.protectionSpace.serverTrust]
        forAuthenticationChallenge:self.challenge];
    [[self.challenge sender] continueWithoutCredentialForAuthenticationChallenge:self.challenge];

    // If User deny request:
    [[self.challenge sender] cancelAuthenticationChallenge:self.challenge];
}
Josef Rysanek
  • 377
  • 3
  • 12