NSURLSession + server with self signed cert NSURLSession + server with self signed cert objective-c objective-c

NSURLSession + server with self signed cert


For me your first example is working fine. I have tested with the following code without problems (it is of course very insecure since it allows any server certificate).

@implementation SessionTest- (void) startSession{    NSURL *url = [NSURL URLWithString:@"https://self-signed.server.url"];    NSURLSessionConfiguration *defaultConfigObject = [NSURLSessionConfiguration defaultSessionConfiguration];    NSURLSession *defaultSession = [NSURLSession sessionWithConfiguration: defaultConfigObject delegate: self delegateQueue: [NSOperationQueue mainQueue]];    NSURLSessionDataTask * dataTask = [defaultSession dataTaskWithURL:url                                                completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {                                                    if(error == nil)                                                    {                                                        NSString * text = [[NSString alloc] initWithData: data encoding: NSUTF8StringEncoding];                                                        NSLog(@"Data: %@",text);                                                    }                                                    else                                                    {                                                        NSLog(@"Error: %@", error);                                                    }                                                }];    [dataTask resume];}- (void)URLSession:(NSURLSession *)session task:(NSURLSessionTask *)task didReceiveChallenge:(NSURLAuthenticationChallenge *)challenge completionHandler:(void (^)(NSURLSessionAuthChallengeDisposition disposition, NSURLCredential *credential))completionHandler{    completionHandler(NSURLSessionAuthChallengeUseCredential, [NSURLCredential credentialForTrust:challenge.protectionSpace.serverTrust]);}@end

Update: This is the class interface, the SessionTest class is the NSURLSessionDataDelegate, to start the data download you create a SessionTest object and call the startSession method.

@interface SessionTest : NSObject <NSURLSessionDelegate>- (void) startSession;@end


There's not enough information to suggest a concrete solution to your problem.

Here are some principal requirements:

Disabling server trust evaluation should work as you tried in your first example. Use this for development only!

See also (https://developer.apple.com/library/ios/documentation/cocoa/Conceptual/URLLoadingSystem/Articles/UsingNSURLSession.html#//apple_ref/doc/uid/TP40013509-SW44)