NSURLSession: How to increase time out for URL requests?

后端 未结 7 1370
粉色の甜心
粉色の甜心 2020-11-27 15:31

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

NSURLSession *session = [NSURLSession sharedSession];

NSURLSessionDat         


        
相关标签:
7条回答
  • 2020-11-27 15:39

    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)
    
    0 讨论(0)
  • 2020-11-27 15:40

    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
    
    0 讨论(0)
  • 2020-11-27 15:45

    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()
    }
    
    0 讨论(0)
  • 2020-11-27 15:49

    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.

    0 讨论(0)
  • 2020-11-27 15:52

    In swift 3. timeout 15 seconds.

        let configuration = URLSessionConfiguration.default
        configuration.timeoutIntervalForRequest = TimeInterval(15)
        configuration.timeoutIntervalForResource = TimeInterval(15)
        let session = URLSession(configuration: configuration)
    
    0 讨论(0)
  • 2020-11-27 15:55

    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.

    0 讨论(0)
提交回复
热议问题