1

I want to change the timeout on my HTTP requests. I'm on a project that uses extensively [NSURLSession sharedSession]. I know that I can't change the configuration of that session (it hasn't one at all).

I know I can define a session with my own config (and I can use as baseline [NSURLSessionConfigurationdefaultSessionConfiguration]), but I don't know how similar is this config to the shared one. The shared one has pre-configured cookie storage policies, cache, etc...

TL;DR I want a session exactly equals to sharedSession, but with a larger timeout. How I can achieve that?

Thanks in advance

webo80
  • 3,365
  • 5
  • 35
  • 52

2 Answers2

3

One option is to not touch the session, but the requests, using either requestWithURL:cachePolicy:timeoutInterval: or by setting timeoutInterval manually (on an NSMutableURLRequest in the latter case, of course).

Otherwise, you can:

  • copy the configuration of the sharedSession, (using copy), and modify it
  • create a new session with this modified configuration, and the same delegate and delegate queue.

Something along the lines of:

NSURLSession *sharedSession = [NSURLSession sharedSession];
NSURLSessionConfiguration *configuration = sharedSession.configuration.copy;
configuration.timeoutIntervalForRequest = whatever;
return [NSURLSession sessionWithConfiguration:configuration delegate:sharedSession.delegate delegateQueue:sharedSession.delegateQueue];

(not tested, but you get the idea).

Of course, you would do this in your own singleton class if you want to reuse the same session.

jcaron
  • 17,302
  • 6
  • 32
  • 46
-1

Sample code for NSURLSessionConfiguration

// Instantiate a session configuration object.

NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration defaultSessionConfiguration];
configuration.timeoutIntervalForRequest = 300;
configuration.HTTPAdditionalHeaders = @{@"Accept" : @"application/xml", @"Content-Type" : @"application/xml; charset=UTF-8", @"User-Agent" : userAgent};

// Instantiate a session object.
NSURLSession *session = [NSURLSession sessionWithConfiguration:configuration];

// Create a data task object to perform the data downloading.
NSURLSessionDataTask *task = [session dataTaskWithURL:[NSURL URLWithString:"http://myexample.com"] completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
                    if (error != nil) {
                        // If any error occurs then just display its description on the console.
                        NSLog(@"Server ERROR: \n "
                              "CODE: %ld, \n "
                              "LOCALIZED DESCRIPTION: %@ \n "


                              "DESCRIPTION: %@ \n "
                              "FAILURE REASON: %@ \n "
                              "RECOVERY OPTION: %@ \n "
                              "RECOVERY SUGGESTION: %@ \n",
                              (long)[error code],
                              [error localizedDescription],
                              error.description,
                              error.localizedFailureReason,
                              error.localizedRecoveryOptions,
                              error.localizedRecoverySuggestion);
                    }
                    else {
                        // If no error occurs, check the HTTP status code.
                        NSInteger HTTPStatusCode = [(NSHTTPURLResponse *)response statusCode];

                        if (HTTPStatusCode == 200) {
                            //Initialize XML parsing with the response data



                            /*NSString *str = [[NSString alloc]initWithData: data encoding: NSUTF8StringEncoding];
                            NSLog(@"The URLSession response is: %@ \n", str);*/
                        }
                    }
                }];

                // Resume the task.
                [task resume];
Ramesh_T
  • 1,251
  • 8
  • 19
  • Thanks, but this doesn't answer my question. The majority of your code is showing me a NSURLSessionDataTask, what has nothing to do in my question. I have the problem in the session configuration, not in the task. And your configuration is pretty standard – webo80 Jan 12 '16 at 13:02
  • I think you are looking something for this. [[[NSURLSession sharedSession]configuration]setTimeoutIntervalForRequest:100]; – Ramesh_T Jan 12 '16 at 13:07
  • No, this won't have any effect, the shared session **isn't** configurable – webo80 Jan 12 '16 at 13:08