1

I'm trying to connect to an ASP.NET MVC server from iOS but since I am running it locally I don't have a real certificate for it. I can browse to the HTTPS URL I am connecting to, but Simulator throws errors when connecting via my Objective-C code. How can I bypass these SSL errors? I need to test some server-side stuff based on the encoding/connections sent from the Simulator.

I have this error:

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

I tried this:

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

But it didn't work. My logic is pretty simple, I just use your standard NSURLSessionDataTask objects:

NSURLSessionDataTask *getTask = [self.session dataTaskWithRequest:getRequest completionHandler: ^(NSData *data, NSURLResponse *response, NSError *error) {

}];

I also tried using NSURLSessionDelegate:

self.session = [NSURLSession sessionWithConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration] delegate:self delegateQueue:[NSOperationQueue mainQueue]];

...

- (void)URLSession:(NSURLSession *)session didReceiveChallenge:(NSURLAuthenticationChallenge *)challenge completionHandler:(void (^)(NSURLSessionAuthChallengeDisposition disposition, NSURLCredential *credential))completionHandler {
    NSURLCredential *credential = [NSURLCredential credentialForTrust:challenge.protectionSpace.serverTrust];
    completionHandler(NSURLSessionAuthChallengeUseCredential, credential);
}

But didReceiveChallenge is never called...

I even tried installing the certificate in Simulator by dragging it in PLUS browsing to my server in Safari and trusting the certificate it presented, but even that didn't work.

Alexandru
  • 12,264
  • 17
  • 113
  • 208

1 Answers1

3

Most answer prepares

First set Allow Arbitrary Loads of Value to YES

First Image

Then set NSExceptionAllowsInsecureHTTPLoads & NSIncludesSubdomains with Value to YES

enter image description here

NSURLSession "HTTP load failed kCFStreamErrorDomainSSL, -9813 ; Self signing certificate

HTTP Connection Error

NSURLSession/NSURLConnection HTTP load failed on iOS 9

For SSL error, you have to add following code in app delegate

+ (BOOL)allowsAnyHTTPSCertificateForHost:(NSString *)host
{
   return YES;
}

NSURLConnection/CFURLConnection HTTP load failed (kCFStreamErrorDomainSSL, -9813) iOS

Can't Receive JSON Request Via HTTPS Due To Untrusted Certificate

Adding a self-signed certificate to iphone Simulator?

Finally Search results shows all answers

Community
  • 1
  • 1
user3182143
  • 9,459
  • 3
  • 32
  • 39