如何使Web服务通过NSURLConnection的连接传递错误:didFailWithError :? [英] How to make webservice pass errors through NSURLConnection's connection:didFailWithError:?

查看:35
本文介绍了如何使Web服务通过NSURLConnection的连接传递错误:didFailWithError :?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Web服务需要做什么才能使 NSURLConnection的委托接收 connection:didFailWithError:消息?

What does a web service need to do to cause NSURLConnection's delegate to receive the connection:didFailWithError: message?

例如:iOS应用将令牌传递给Web服务,Web服务会查找令牌,然后Web服务需要响应并显示无效令牌"之类的错误.

For example: iOS app passes a token to the web service, web service looks up the token, and then the web service needs to respond with an error saying "invalid token" or something of the like.

当前,已接收数据,并在" connectionDidFinishLoading :"中解析错误消息.这意味着我要在两个地方进行错误检查,这是我想避免的地方.

Currently, the data is received, and in "connectionDidFinishLoading:" is parsed for error messages. This means I'm error checking in two places, which I am trying to avoid.

我完全可以控制iOS应用和网络服务.

I have both the iOS app and web service completely under my control.

推荐答案

以我的经验(编程中最危险的三个词),仅当HTTP交换失败时才调用 -connection:didFailWithError:.这通常是网络错误,也可能是身份验证错误(我不使用身份验证).如果HTTP消息成功,则无论响应代码 -connectionDidFinishLoading:都被调用.

In my experience (the three most dangerous words in programming), -connection:didFailWithError: is only called if the HTTP exchange failed. This is usually a network error or maybe an authentication error (I don't use authentication). If the HTTP message succeeds, no matter the response code, -connectionDidFinishLoading: is called.

我的解决方案:当我检测到错误时,调用 -connection:didFailWithError:.这样,我所有的错误处理代码都集中在一个地方.

My solution: call -connection:didFailWithError: when I detected an error. That way all my error handling code is in one place.

在我的 -connectionDidFinishLoading:顶部,我有:

NSError *error;
NSDictionary *result = [self parseResultWithData:self.connectionData error:&error];
if (!result) {
    [self connection:connection didFailWithError:error];
    return;
}

这篇关于如何使Web服务通过NSURLConnection的连接传递错误:didFailWithError :?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆