Note that there are some explanatory texts on larger screens.

plurals
  1. PONSURLSession + server with self signed cert
    text
    copied!<p>I have an app that is production along with a development server that has a <strong>self signed certificate.</strong> </p> <p>I am attempting to test <code>NSURLSession</code> and background downloading but can't seem to get past <code>- (void)URLSession:(NSURLSession *)session didReceiveChallenge:(NSURLAuthenticationChallenge *)challenge completionHandler:(void (^)(NSURLSessionAuthChallengeDisposition disposition, NSURLCredential *credential))completionHandler</code></p> <p>When I use <code>NSURLConnection</code> I am able to bypass it using: </p> <pre><code>- (BOOL)connection:(NSURLConnection *)connection canAuthenticateAgainstProtectionSpace:(NSURLProtectionSpace *)protectionSpace { NSLog(@"canAuthenticateAgainstProtectionSpace %@", [protectionSpace authenticationMethod]); return [protectionSpace.authenticationMethod isEqualToString:NSURLAuthenticationMethodServerTrust]; } - (void)connection:(NSURLConnection *)connection didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge { NSLog(@"didReceiveAuthenticationChallenge %@ %zd", [[challenge protectionSpace] authenticationMethod], (ssize_t) [challenge previousFailureCount]); [challenge.sender useCredential:[NSURLCredential credentialForTrust:challenge.protectionSpace.serverTrust] forAuthenticationChallenge:challenge]; } </code></pre> <p>But I can't figure out how to get this to work with <code>NSURLSession</code> >:(</p> <p>This is what I have currently (that doesn't work): </p> <pre><code>- (void)URLSession:(NSURLSession *)session didReceiveChallenge:(NSURLAuthenticationChallenge *)challenge completionHandler:(void (^)(NSURLSessionAuthChallengeDisposition disposition, NSURLCredential *credential))completionHandler { NSLog(@"NSURLSession did receive challenge."); completionHandler(NSURLSessionAuthChallengeUseCredential, [NSURLCredential credentialForTrust:challenge.protectionSpace.serverTrust]); } </code></pre> <p>I also tried creating a category of <code>NSURLSession</code> that would allow any certificate for a host: </p> <pre><code>#import "NSURLRequest+IgnoreSSL.h" @implementation NSURLRequest (IgnoreSSL) + (BOOL)allowsAnyHTTPSCertificateForHost:(NSString*)host { return YES; } + (void)setAllowsAnyHTTPSCertificate:(BOOL)allow forHost:(NSString*)host {} @end </code></pre> <p>Which also doesn't seem to help. </p> <hr> <p><strong>EDIT</strong></p> <p>I've updated this method to return: </p> <pre><code>- (void)URLSession:(NSURLSession *)session didReceiveChallenge:(NSURLAuthenticationChallenge *)challenge completionHandler:(void (^)(NSURLSessionAuthChallengeDisposition disposition, NSURLCredential *credential))completionHandler { //Creates credentials for logged in user (username/pass) NSURLCredential *cred = [[AuthController sharedController] userCredentials]; completionHandler(NSURLSessionAuthChallengeUseCredential, cred); } </code></pre> <p>Which still does nothing. </p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload