NSURLSession: How to increase URL request timeout?

I am using iOS 7 new NSURLSessionDataTask to retrieve data as follows:

 NSURLSession *session = [NSURLSession sharedSession]; NSURLSessionDataTask *dataTask = [session dataTaskWithRequest: request completionHandler: ^(NSData *data, NSURLResponse *response, NSError *error) { // }]; 

How to increase timeout values ​​to avoid the error "The request timed out" (in NSURLErrorDomain Code = -1001 )?

I checked the documentation for NSURLSessionConfiguration , but did not find a way to set the timeout value.

Thank you for your help!

+60
ios objective-c swift nsurlconnection
May 2 '14 at 12:59
source share
7 answers

Objc

 NSURLSessionConfiguration *sessionConfig = [NSURLSessionConfiguration defaultSessionConfiguration]; sessionConfig.timeoutIntervalForRequest = 30.0; sessionConfig.timeoutIntervalForResource = 60.0; 

Swift

 let sessionConfig = URLSessionConfiguration.default sessionConfig.timeoutIntervalForRequest = 30.0 sessionConfig.timeoutIntervalForResource = 60.0 let session = URLSession(configuration: sessionConfig) 

What do the documents say

timeoutIntervalForRequest and timeoutIntervalForResource specify the timeout interval for the request as well as the resource.

timeoutIntervalForRequest . The timeout interval to use while waiting to receive additional data. The timer associated with this value is reset when new data arrives. When the request timer reaches the specified interval without receiving any new data, it starts a timeout.

timeoutIntervalForResource - the maximum time during which a resource request must be allowed. This value determines how to wait for the transfer of the entire resource before surrender. the resource timer starts when the request is initiated and calculated until either the request ends or this timeout interval is reached, whichever comes first.

Based on NSURLSessionConfiguration class reference

+103
May 02 '14 at
source share
β€” -

In the case of the Swift developer

for this you need to use

  let urlconfig = NSURLSessionConfiguration.defaultSessionConfiguration() urlconfig.timeoutIntervalForRequest = 12 urlconfig.timeoutIntervalForResource = 12 self.session = NSURLSession(configuration: urlconfig, delegate: self.delegates, delegateQueue: nil) 
+16
May 24, '15 at 19:00
source share

In fast 3. timeout mode 15 seconds.

  let configuration = URLSessionConfiguration.default configuration.timeoutIntervalForRequest = TimeInterval(15) configuration.timeoutIntervalForResource = TimeInterval(15) let session = URLSession(configuration: configuration) 
+8
Feb 03 '17 at 9:41
source share

NSURLSessionConfiguration includes the timeoutIntervalForRequest property:

 @property NSTimeInterval timeoutIntervalForRequest 

to control the timeout interval. There is also a timeoutIntervalForResource for the timeout after the request is run.

+4
May 02 '14 at 13:03
source share

In SWIFT 3.0 you need to use

  if let cleanURLString = urlString.addingPercentEncoding(withAllowedCharacters: CharacterSet.urlQueryAllowed), let theURL: Foundation.URL = Foundation.URL(string: cleanURLString) { var task:URLSessionDataTask? let urlconfig = URLSessionConfiguration.default urlconfig.timeoutIntervalForRequest = 20 urlconfig.timeoutIntervalForResource = 60 let session = Foundation.URLSession(configuration: urlconfig, delegate: self, delegateQueue: OperationQueue.main) let request = URLRequest(url: theURL) request.httpMethod = "POST" request.cachePolicy = URLRequest.CachePolicy.reloadIgnoringCacheData request.setValue("application/json; charset=utf-8", forHTTPHeaderField: "Content-Type") let paramString = YourParameterString request.httpBody = paramString.data(using: String.Encoding.utf8) task = session.dataTask(with: request) { (data, response, error) in // Do here }) dataTask.resume() } 
+2
Nov 04 '16 at 8:28
source share

For Swift 4:

 let config = URLSessionConfiguration.default config.timeoutIntervalForRequest = TimeInterval(15) config.timeoutIntervalForResource = TimeInterval(15) let urlSession = URLSession(configuration: config) 

TimeoutIntervalForRequest is for all tasks in sessions based on this configuration. TimeoutIntervalForResource is for all tasks in sessions based on this configuration.

+1
Dec 26 '17 at 12:45
source share

In my case, I increased the wait time of the wrong class. The timeout error was resolved by increasing the timeout for URLRequest , not URLSession

 var request = URLRequest(url: url) request.timeoutInterval = 30 
0
Feb 12 '19 at 1:52
source share



All Articles