我正在开发我的第一个 iOS 应用程序并使用 Alamofire 进行网络调用。这很好也很容易。我在单独的类中实现了 Alamofire,我们称它为 WebserviceHelper。现在我在整个应用程序中使用这个类。但现在我想设置超时。
这是关于我如何在 WebserviceHelper 类中实现功能的片段。
static func requestService(methodName: String, method: HTTPMethod, parameters: Parameters, headers: HTTPHeaders? = nil, showLoaderFlag: Bool, viewController: UIViewController, completion: @escaping (_ success: JSON) -> Void) {
let reachability = Reachability()!
/*
checking for internet connection
*/
if(reachability.connection != .none) {
/*
Showing Loader here
*/
if(showLoaderFlag){
ViewControllerUtils.customActivityIndicatory(viewController.view, startAnimate: true)
}
print("Web Service Url - \(Common.URL+methodName)")
print("Parameters - \(parameters)")
/*
Request configure
*/
Alamofire.request(Common.URL+methodName, method: method, parameters: parameters, encoding: JSONEncoding.default, headers: headers) .responseJSON { response in
在这里您可以看到它安静且易于使用。我使用了很多方法来设置超时,但它对我不起作用。 this是我试过的。还有this链接,但对我来说没什么用。
你们能帮我用 Alamofire 设置超时吗?
最佳答案
您应该尝试在最新的 Alamofire 中运行的 URLRequest
,因为我已经实现了它。见以下代码:
guard let url = URL(string: "") else { return }
var urlRequest = URLRequest(url: url)
urlRequest.timeoutInterval = TimeInterval(exactly: 30)! // Set timeout in request of 30 seconds
Alamofire.request(urlRequest).response { (dataResponse) in
}
希望对您有所帮助。
关于ios - Alamofire如何设置超时,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54478800/