ios - Alamofire如何设置超时

标签 ios swift alamofire

我正在开发我的第一个 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/

相关文章:

ios - 为动画添加 UIImage 到 NSMutable 数组

iphone - Cocos2d iPhone 中的 NSNotification

ios - 接受函数的回调作为参数

generics - 在 Swift 中扩展泛型?

ios - Alamofire 缓存问题

json - 使用 NSJSONSerialization 从 json 响应构建对象

ios - 拖动我的 MKAnnotationView 后拖动 map 不会随之移动 MKAnnotationView

ios - 如何让MPMoviePlayerController忽略静音开关

ios - Swift:未将属性添加到 ProductName-swift.h 文件

ios - 如何使用 Alamofire 将带有一些键和值的 UIImage 上传到服务器