【问题标题】:AFNetworking identify reason of failure: User cancel request or network failureAFNetworking 识别失败原因:用户取消请求或网络故障
【发布时间】:2014-03-27 07:54:21
【问题描述】:
我正在使用AFNetworking 发出网络请求。请求失败块failure:^(AFHTTPRequestOperation *operation, NSError *error)在两种情况下被调用,
- 当请求因网络连接问题而失败时。
- 当我取消正在进行的请求时。
在故障块代码中,我无法区分上述两个原因。仅当请求因网络问题而失败时,我才需要区分以提供自定义通知。
如何正确识别请求失败的原因?
【问题讨论】:
标签:
ios
http
networking
request
afnetworking
【解决方案1】:
我相信error.code 的值将等于方案 1 中的 NSURLErrorNotConnectedToInternet (-1009) 和方案 2 中的 NSURLErrorCancelled (-999)。
这是一个例子:
failure:^(AFHTTPRequestOperation *operation, NSError* error) {
if(error.code == NSURLErrorNotConnectedToInternet) {
// handle scenario 1
} else if(error.code == NSURLErrorCancelled) {
// handle scenario 2
} else {
// handle unexpected errors
}
}