How to make webservice pass errors through NSURLConnection's connection:didFailWithError:?

坚强是说给别人听的谎言 提交于 2019-12-21 19:34:53

问题


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

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.

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.

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


回答1:


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.

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

At the top of my -connectionDidFinishLoading:, I have:

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



回答2:


There are many conditions on which the delegate connection:didFailWithError: of NSUrlConnection may invoke.Here's a list of those errors or constants.I think an alertview would be better to show http errors in connection:didFailWithError:.

-(void) connection:(NSURLConnection *)connection   didFailWithError: (NSError *)error 
{   
UIAlertView *errorAlert= [[UIAlertView alloc] initWithTitle: [error localizedDescription] message: [error localizedFailureReason] delegate:nil                  cancelButtonTitle:@"Done" otherButtonTitles:nil];  
[errorAlert show];   
[errorAlert release];     
NSLog (@"Connection Failed"); 
} 



回答3:


While not directly related to your question, I would encourage you to move to a more high level library. I can heartily recommend AFNetworking, it is production ready and I have used it in many projects. This will allow you to inspect the response code of each request in the failure block. This project also abstracts away a lot of the low level handling that you would otherwise be required to write for network communication; I'm speaking here about parsing and creating XML / JSON strings to communicate with a service.

To give you a more focused answer to your question, I would call the cancel method of your NSURLConnection once you have noticed an error in connectionDidFinishLoading:. This will automatically cancel the request and call the failure method of the delegate object.

The documentation for NSURLConnection is pretty dry, and the failure method of the delegate does not specifically document the failure cases. You may be able to find more information in the URL Loading System Programming Guide.




回答4:


I couldn't see the forrest for the trees.

I needed to step back from connection:didFailWithError: and look at a different delegate method connection:didReceiveResponse:!!

With the web service fully under my control, the endpoint could respond with a 500 status code, which gets picked up in connection:didReceiveResponse, and pass along some JSON further explaining the situation, which gets picked up and processed in connection:didReceiveData:.

The NSURLConnection delegate hangs onto a couple more bits of state throughout the process, but it has the best code smell I've found so far.

Jeffery's answer was by far most correct: the connection:didFailWithError: callback is only in relation to the network failing, any response from the web service means the connection didn't fail!



来源:https://stackoverflow.com/questions/10398448/how-to-make-webservice-pass-errors-through-nsurlconnections-connectiondidfailw

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!