NSURLSession: How to increase time out for URL requests? NSURLSession: How to increase time out for URL requests? ios ios

NSURLSession: How to increase time out for URL requests?


ObjC

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

Swift

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

What docs say

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

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

timeoutIntervalForResource - The maximum amount of time that a resource request should be allowed to take. This value controls how long to wait for an entire resource to transfer before giving up. The resource timer starts when the request is initiated and counts until either the request completes or this timeout interval is reached, whichever comes first.

Based on NSURLSessionConfiguration Class Reference


In case Swift developer coming here

to do 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)


In swift 3. timeout 15 seconds.

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