Set timeout in Alamofire

后端 未结 14 1283
爱一瞬间的悲伤
爱一瞬间的悲伤 2020-12-01 03:33

I am using Alamofire 4.0.1 and I want to set a timeout for my request. I tried the solutions gived in this question:

In the first case

14条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-12-01 04:12

    Based in @kamal-thakur response.

    Swift 3:

    var request = URLRequest(url: NSURL.init(string: "YOUR_URL") as! URL)
    request.httpMethod = "POST"
    request.setValue("application/json", forHTTPHeaderField: "Content-Type")
    request.timeoutInterval = 10 // 10 secs
    let postString = "param1=\(var1)¶m2=\(var2)"
    request.httpBody = postString.data(using: .utf8)
    Alamofire.request(request).responseJSON {
        response in
        // do whatever you want here
    }
    

提交回复
热议问题