NSURLSession + server with self signed cert

前端 未结 2 659
南旧
南旧 2020-12-23 20:30

I have an app that is production along with a development server that has a self signed certificate.

I am attempting to test NSURLSession

相关标签:
2条回答
  • 2020-12-23 21:18

    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
    
    0 讨论(0)
  • 2020-12-23 21:18

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

    Here are some principal requirements:

    • Since you want a background task, ensure you created a suitable NSSession object through backgroundSessionConfiguration:. Using this class factory method is mandatory for getting background sessions.

    • For requests running in the background in a separate process, only upload and download tasks are supported. Note that, in your original code, you are using a data task.

    • Ensure you have properly implemented the delegate method application:handleEventsForBackgroundURLSession:completionHandler: in your App Delegate. When your app is not running, and when the session is running in its own process and requires credentials, iOS will restart your app in the background and the background session will call this delegate method. See also https://developer.apple.com/library/ios/documentation/UIKit/Reference/UIApplicationDelegate_Protocol/Reference/Reference.html#//apple_ref/occ/intfm/UIApplicationDelegate/application:handleEventsForBackgroundURLSession:completionHandler:

    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)

    0 讨论(0)
提交回复
热议问题