ios - AFNetworking 识别失败原因 : User cancel request or network failure

标签 ios http networking request afnetworking

我正在使用 AFNetworking 发出网络请求。请求失败 block failure:^(AFHTTPRequestOperation *operation, NSError *error) 在两种情况下被调用,

  1. 当请求因网络连接问题而失败时。
  2. 当我取消正在进行的请求时。

在失败 block 代码中,我无法区分上述 2 个原因。我需要区分仅在由于网络问题导致请求失败时才提供自定义通知。

如何正确判断请求失败的原因?

最佳答案

我相信在场景 1 中 error.code 的值将等于 NSURLErrorNotConnectedToInternet (-1009) 和 NSURLErrorCancelled (-999 ) 在场景 2 中。

这是一个例子:

failure:^(AFHTTPRequestOperation *operation, NSError* error) {                  
    if(error.code == NSURLErrorNotConnectedToInternet) {
        // handle scenario 1
    } else if(error.code == NSURLErrorCancelled) {
        // handle scenario 2
    } else {
        // handle unexpected errors
    }         
}

关于ios - AFNetworking 识别失败原因 : User cancel request or network failure,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22681333/

相关文章:

ios - View Controller 在 Segue 后消失

ios - 如何确定哪个 View Controller 当前处于事件状态/显示 View 的 View Controller ?

ios - 如何检查网络是否断开显示警报并停止该方法

javascript - AngularJS HTTP请求不会出现在错误中

c++ - recvfrom()没有得到一切

networking - Haxe+实时网络

ios - scrollViewDidScroll 在设置表偏移后被调用 "twice"

iOS客户端: I can have user sign into Active Directory,但是如何访问MS Graph API?

java - 为什么 PoolingHttpClientConnectionManager 提供越来越慢的连接?

c# - 测量下载速度