Alamofire如何设置超时

问题描述 投票:0回答:1

我正在开发我的第一个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设置Timeout吗?

ios swift alamofire
1个回答
1
投票

您应该尝试使用最新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

}

我希望这能帮到您。

© www.soinside.com 2019 - 2024. All rights reserved.