0

Scenario: Authenticating a user. Grab the cookie, set the cookie for other requests. Fine. What I can't immediately figure out is how to handle when the authentication fails without push my next view onto the screen. if my response is {errors:{password:does not match}} should i parse this response too and create a condition accordingly? Should I simply check the response code? I'm making this to difficult, I can tell. I added my next view in:

  • (void)connectionDidFinishLoading:(NSURLConnection *)connection

so, when the connection finishes, it naturally pushes the next view. I'd like to prevent it from ever getting here if the authentication fails too. Maybe I shouldn't push my next view from here. Where then?

2 Answers2

1

I check the redirect path: This shows me if the authentication went fine or not.

- (NSURLRequest*)connection:(NSURLConnection*)connection willSendRequest:(NSURLRequest*)request redirectResponse:(NSURLResponse*)redirectResponse
{

    NSLog(@"willSendRequest (from %@ to %@)", redirectResponse.URL, request.URL);
    NSString *from =   [NSString stringWithFormat:@"%@",redirectResponse.URL];
    NSString *to =   [NSString stringWithFormat:@"%@",request.URL];
    if([from isEqualToString:serverSignInPath] )
      //logged=true
    else
     //logged=false
}
Mobile Developer
  • 5,730
  • 1
  • 39
  • 45
  • Yeah that's cool. I read something similar. Either solution works I suppose; Redirect or StatusCode, I just had to think it through for a minute. –  Jul 29 '11 at 04:06
0
NSURLResponse *HTTPResponse = (NSURLResponse *)response;
NSInteger statusCode = [HTTPResponse statusCode];

if (404 == statusCode || 500 == statusCode) {
    [connection cancel];
    [(MyAppDelegate *)[[UIApplication sharedApplication] delegate] setNetworkActivityIndicatorVisible:NO];
}

This way, it never gets to connectionDidFinishLoading to push the next view.

Rails response should look like this.

render :json => @foo.to_json, :status => 404