Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Extract data from NSURLResponse

Tags:

ios

I have this sweet method that authenticates a user, however I can't figure out how to get the status code of the request. I can clearly see it exists when I do NSLog(@"%@", response);, but I can't find any way to pull it out of that. Is there a method or do I have to parse it myself somehow?

- (BOOL)authenticateUserWithEmail:(NSString *)email password:(NSString *)password
{
    NSURL *url = [[NSURL alloc] initWithString:[NSString stringWithFormat:
                                         @"%@/users/sign_in.json?user[email]=%@&user[password]=%@&user[remember_me]=true",
                                         LURL,
                                         email,
                                         password]];
    NSMutableURLRequest *urlRequest = [[NSMutableURLRequest alloc] initWithURL:url];
    [urlRequest setHTTPMethod:@"POST"];
    NSURLResponse *response;
    NSError *error;
    NSData *responseData = [NSURLConnection sendSynchronousRequest:urlRequest returningResponse:&response error:&error];
    NSLog(@"Response: %@", [[NSString alloc] initWithData:responseData encoding:NSASCIIStringEncoding] );
    NSLog(@"Response Meta: %@", response);
    return (error == NULL);
}
like image 460
OneChillDude Avatar asked Sep 23 '13 01:09

OneChillDude


1 Answers

Try this

NSHTTPURLResponse *HTTPResponse = (NSHTTPURLResponse *)response;
NSInteger statusCode = [HTTPResponse statusCode];
like image 138
Frank Avatar answered Oct 17 '22 00:10

Frank