7
votes

I am sending a post request using the following code in iOS 9 to a https server

[NSURLConnection sendSynchronousRequest:request returningResponse:&requestResponse error:&err];  

But I get the following error

CFNetwork SSLHandshake failed (-9824)
NSURLSession/NSURLConnection HTTP load failed (kCFStreamErrorDomainSSL, -9824)

I have tried adding the exception to info.plist as follows:

<key>NSAppTransportSecurity</key>  
<dict>
    <key>NSExceptionDomains</key>
    <dict>
    <key>www.myserver.com</key>
    <dict>
    <key>NSIncludesSubdomains</key>
    <true/>
    <key>NSTemporaryExceptionAllowsInsecureHTTPLoads</key>
    <true/>
    <key>NSTemporaryExceptionMinimumTLSVersion</key>
    <string>TLSv1.1</string>
</dict>

I also tried

<key>NSAppTransportSecurity</key>
   <dict>
     <key>NSAllowsArbitraryLoads</key>
     <true/>
   </dict>

It works on a real device but not on a simulator

2

2 Answers

4
votes
  1. From NSURLConnection to NSURLSession worked for me

I was able to solve as following( NSURLConnection is deprecated and you need to use NSURLSession) :

NSURL *URL = [NSURL URLWithString:@"http://example.com"];
NSURLRequest *request = [NSURLRequest requestWithURL:URL];

[NSURLConnection sendAsynchronousRequest:request
                                queue:[NSOperationQueue mainQueue]
                    completionHandler:^(NSURLResponse *response, NSData  *data, NSError *error) {
 // ... 
}];

converted to:

NSURL *URL = [NSURL URLWithString:@"http://example.com"];
NSURLRequest *request = [NSURLRequest requestWithURL:URL];

NSURLSession *session = [NSURLSession sharedSession];
NSURLSessionDataTask *task = [session dataTaskWithRequest:request
                                     completionHandler:
 ^(NSData *data, NSURLResponse *response, NSError *error) {
     // ...
  }];

[task resume];

From NSURLConnection to NSURLSession

  1. Also included in Info.plist see documentation:

Info.plist reference

<key>NSAppTransportSecurity</key>
<dict>
  <key>NSExceptionDomains</key>
  <dict>
  <key>yourdomain.net</key>
  <dict>
  <key>NSIncludesSubdomains</key>
  <true/>
  <key>NSTemporaryExceptionAllowsInsecureHTTPLoads</key>
  <true/>
  <key>NSTemporaryExceptionMinimumTLSVersion</key>
  <string>1.2</string>
  <key>NSTemporaryExceptionRequiresForwardSecrecy</key>
  <false/>
  </dict>
  </dict>
</dict>
  1. And ultimately

Announcement: CFNetwork SSLHandshake failed (-9824) while integrating Login with Amazon SDK for iOS Back to Category Back to Category

CFNetwork SSLHandshake failed (-9824) while integrating Login with Amazon SDK for iOS Back to Category Back to Category

Just change to yourdomain.net from api.amazon.com

Hope it helps.

2
votes

Doing following solved my issue:

  1. Add/Edit in info.plist

<key>NSAppTransportSecurity</key> <dict> <key>NSExceptionDomains</key> <dict> <key>yourdomain.com</key> <dict> <key>NSIncludesSubdomains</key> <true/> <key>NSTemporaryExceptionAllowsInsecureHTTPLoads</key> <true/> <key>NSTemporaryExceptionMinimumTLSVersion</key> <string>1.2</string> <key>NSTemporaryExceptionRequiresForwardSecrecy</key> <false/> </dict> </dict> </dict>

  1. Add following code in your class which delegates NSURLConnection

- (BOOL)connection:(NSURLConnection *)connection canAuthenticateAgainstProtectionSpace:(NSURLProtectionSpace *)protectionSpac { return YES; }

- (void)connection:(NSURLConnection *)connection didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge {
    NSArray *trustedHosts = [NSArray arrayWithObjects:@"mytrustedhost",nil];

    if ([challenge.protectionSpace.authenticationMethod isEqualToString:NSURLAuthenticationMethodServerTrust]){
        if ([trustedHosts containsObject:challenge.protectionSpace.host]) {
            [challenge.sender useCredential:[NSURLCredential credentialForTrust:challenge.protectionSpace.serverTrust] forAuthenticationChallenge:challenge];
        }
    }
    [challenge.sender continueWithoutCredentialForAuthenticationChallenge:challenge];
}

Hope this will help you.