2013-09-23 38 views
3

我有這個可靠的方法來驗證用戶,但我無法弄清楚如何獲取請求的狀態碼。當我做NSLog(@"%@", response);時,我可以清楚地看到它存在,但我無法找到任何方法將其解決。有沒有一種方法,或者我必須以某種方式解析它?從NSURLResponse提取數據

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

回答

9

試試這個

NSHTTPURLResponse *HTTPResponse = (NSHTTPURLResponse *)response; 
NSInteger statusCode = [HTTPResponse statusCode]; 
+0

這看起來像我需要什麼,你能解釋一下這行'NSHTTPURLResponse *類HTTPResponse =(NSHTTPURLResponse *)響應;' – OneChillDude

+2

基本上你告訴系統把響應爲類型爲NSHTTPURLResponse。由於來自NSURLResponse的NSHTTPURLResponse inherts是可能的。 附加NSHTTPURLResponse statusCode方法。 – Frank

+0

這太棒了,我試圖轉換爲'NSHTTPURLResponse',但我得到了一個錯誤。謝謝! – OneChillDude