NSURLSession: How to increase time out for URL requests?

IosObjective CSwiftHttpNsurlsession

Ios Problem Overview


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

NSURLSession *session = [NSURLSession sharedSession];

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

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

I have checked the documentation for NSURLSessionConfiguration but did not find a way to set the time out value.

Thank you for your help!

Ios Solutions


Solution 1 - Ios

###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 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

Solution 2 - Ios

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)

Solution 3 - Ios

In swift 3. timeout 15 seconds.

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

Solution 4 - Ios

In my case I was increasing the timeout of the wrong class. My timeout error was solved by increasing the timeout of the URLRequest not the URLSession

var request = URLRequest(url: url)
request.timeoutInterval = 30

Solution 5 - Ios

NSURLSessionConfiguration includes the property timeoutIntervalForRequest:

@property NSTimeInterval timeoutIntervalForRequest

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

Solution 6 - Ios

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()
}

Solution 7 - Ios

For Swift 4:

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

The timeoutIntervalForRequest is for all tasks within sessions based on this configuration. The timeoutIntervalForResource is for all tasks within sessions based on this configuration.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionAlexRView Question on Stackoverflow
Solution 1 - IosRafa de KingView Answer on Stackoverflow
Solution 2 - IosSruit A.SukView Answer on Stackoverflow
Solution 3 - IosDanil ShaykhutdinovView Answer on Stackoverflow
Solution 4 - IosNico DiosoView Answer on Stackoverflow
Solution 5 - IosWojtek SurowkaView Answer on Stackoverflow
Solution 6 - IosLineesh K MohanView Answer on Stackoverflow
Solution 7 - Iosuser9060380View Answer on Stackoverflow