0

I am using this way API to login in my app.

NSString *url = [NSString stringWithFormat:@"%@//login",xyz];
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:url]];


    NSError *error;
    NSData *jsonData = [NSJSONSerialization dataWithJSONObject:dict options:NSJSONWritingPrettyPrinted error:&error];
    if (!jsonData) {
        NSLog(@"Error creating JSON object: %@", [error localizedDescription]);
    }


    [request setValue:@"application/json;charset=utf-8" forHTTPHeaderField:@"Content-Type"];
    [request setValue:APIKEY forHTTPHeaderField:@"X_API_KEY"];

    [request setHTTPMethod:@"POST"];
    [request setHTTPBody:jsonData];

    [NSURLConnection sendAsynchronousRequest:request
     // the NSOperationQueue upon which the handler block will be dispatched:
                                       queue:[NSOperationQueue mainQueue]
                           completionHandler:^(NSURLResponse *response, NSData *data, NSError *error)
     {
         NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse *)response;

         NSDictionary *responseDict = [NSJSONSerialization JSONObjectWithData: data options: 0 error: &error];  //I am using sbjson to parse

         if(httpResponse.statusCode == 200)
         {

             //Show message to user success
         }
         else  if(httpResponse.statusCode == 401)
         {
            //Show message to user -fail
         }
         else if(httpResponse.statusCode == 500)
         {
             //Show message to user- server error
         }
     }];

When I use correct user name and password, I get httpResponse and status code as 200. But if use wrong user name and password, I am not getting 401.

So how to handle this situation

Regards Ranjit

Guilherme
  • 7,839
  • 9
  • 56
  • 99
Ranjit
  • 4,576
  • 11
  • 62
  • 121
  • 401 sounds like a reasonable response to bad credentials, but what makes you certain that this is the server's design? Maybe the response body contains a clue. – danh Feb 09 '15 at 15:51
  • @danh, as you san see in the above code, I was checking it based on the status code, and as of now I dont get any status code, when credentials are wrong, I do get error code :104 in reponse. So you suggest me to use that while checking? – Ranjit Feb 10 '15 at 06:28

1 Answers1

0

You are casting your NSURLResponse to an NSHTTPURLResponse but this won't automatically expose the actual value you expect in the statusCode property.

Instead, you should check if the error property has been populated and check the code property instead.

if(error)
{
    NSLog(@"error: %@", error.code);

    if(error.code == 401)
    {
        //handle 401 errors
    }
    else if(error.code == 500)
    {
        //handle 500 errors
    }
}
else
{
    //successful request
}
Zack Brown
  • 5,990
  • 2
  • 41
  • 54
  • The error codes should actually be `kCFURLErrorUserCancelledAuthentication` for 401 and `kCFURLErrorBadServerResponse` for 500 – Guilherme Feb 10 '15 at 12:22