Search code examples
nsurlconnectionnshttpcookie

NSURLConnection and NSHTTPCookie


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?


Solution

  • 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
    }