参考https://stackoverflow.com/questions/41803856/set-timeout-in-alamofire?answertab=votes#tab-top
Alamofire 设置超时时间,监听超时回调
let timeout = 10//10 secs
guard let requestURL = URL.init(string: url) else {
return
}
let parameters: [String: Any] = []
var urlRequest = URLRequest.init(url: requestURL, cachePolicy: .useProtocolCachePolicy, timeoutInterval: timeout)
urlRequest.httpMethod = "POST"
urlRequest.setValue("application/json", forHTTPHeaderField: "Content-Type")
urlRequest.httpBody = try! JSONSerialization.data(withJSONObject: parameters, options: [])
Alamofire.request(urlRequest).responseJSON { response in
switch (response.result) {
case .success:
//request success
break
case .failure(let error):
if error._code == NSURLErrorTimedOut {
//request timeout
} else {
//request failed
}
}
}
网友评论