如何实现“可配置”NSURLSession共享会话?

时间:2016-01-12 12:50:38

标签: ios objective-c nsurlsession

我想更改HTTP请求的超时。我正在使用广泛使用[NSURLSession sharedSession]的项目。我知道我无法更改该会话的配置(它根本没有)。

我知道我可以使用自己的配置定义会话(我可以使用基线[ NSURLSessionConfiguration defaultSessionConfiguration]),但我不知道这个配置与共享配置有多相似。共享的已预先配置了cookie存储策略,缓存等...

TL; DR我希望会话完全等于sharedSession,但是超时更长。我如何实现这一目标?

提前致谢

2 个答案:

答案 0 :(得分:3)

一种选择是不使用requestWithURL:cachePolicy:timeoutInterval:或手动设置timeoutInterval来触摸会话,而是触摸请求(当然,在后一种情况下,在NSMutableURLRequest上)。< / p>

否则,你可以:

  • 复制sharedSession的配置,(使用copy)并修改
  • 使用此修改后的配置以及相同的委托和委托队列创建新会话。

有些事情:

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

(未经测试,但你明白了。)

当然,如果你想重用同一个会话,你可以在自己的单例类中执行此操作。

答案 1 :(得分:-1)

NSURLSessionConfiguration

的示例代码

//实例化会话配置对象。

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];