I am using iOS 7\'s new NSURLSessionDataTask
to retrieve data as follows:
NSURLSession *session = [NSURLSession sharedSession];
NSURLSessionDat
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 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
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()
}
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.
In swift 3. timeout 15 seconds.
let configuration = URLSessionConfiguration.default
configuration.timeoutIntervalForRequest = TimeInterval(15)
configuration.timeoutIntervalForResource = TimeInterval(15)
let session = URLSession(configuration: configuration)
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.