美文网首页
Alamofire 超时设置

Alamofire 超时设置

作者: yxibng | 来源:发表于2020-06-09 17:59 被阅读0次

    参考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
            }
        }
    }
    

    相关文章

      网友评论

          本文标题:Alamofire 超时设置

          本文链接:https://www.haomeiwen.com/subject/judetktx.html