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.