5

我有这种对用户进行身份验证的好方法,但是我不知道如何获取请求的状态代码。当我这样做时,我可以清楚地看到它的存在NSLog(@"%@", response);,但我找不到任何方法来摆脱它。有没有一种方法或者我必须以某种方式自己解析它?

- (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);
}
4

1 回答 1

13

试试这个

NSHTTPURLResponse *HTTPResponse = (NSHTTPURLResponse *)response;
NSInteger statusCode = [HTTPResponse statusCode];
于 2013-09-23T01:59:45.237 回答