2012-01-24 4 views
0

사용자가 유효한지 보려면 REST API를 누르려고합니다. connectionDidFinishLoading 실행을 알고 있지만 응답 (XML)을 확인하는 방법을 모르겠습니다. 제발 조언.xml post loading xml 응답

signIn 기능은 일반적으로 당신이 NSXMLParser을 사용 XML을 구문 분석

- (IBAction)signIn:(id)sender; 
{ 
    [emailError setHidden:YES]; 

    if([emailAddressTxt text] && [passwordTxt text]) { 
     // send user/pass to server for validation 
     if([self NSStringIsValidEmail:[emailAddressTxt text]]) { 
      NSString *post = [NSString stringWithFormat:@"Email=%@&Password=%@", emailAddressTxt.text, passwordTxt.text]; 
      NSData *postData = [post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES]; 

      NSString *postLength = [NSString stringWithFormat:@"%d", [postData length]]; 

      NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init]; 
      [request setURL:[NSURL URLWithString:@"http://www.mySite.com/validate.php"]]; 
      [request setHTTPMethod:@"POST"]; 
      [request setValue:postLength forHTTPHeaderField:@"Content-Length"]; 
      [request setHTTPBody:postData]; 
      [NSURLConnection connectionWithRequest:request delegate:self]; 
     } 
    } else { 
     // give error dialogue 
     [emailError setText:@"User not found"]; 
     [emailError setHidden:NO]; 
    } 
} 

- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response { 
    //[signInData setLength:0]; 
} 

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)d { 
    //[signInData appendData:d]; 
    // updated to: 
    signInData = (NSMutableData *)d; 
} 

- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error { 
    // Fail.. 
    [emailError setText:@"Connection Error"]; 
    [emailError setHidden:NO]; 
} 

- (void)connectionDidFinishLoading:(NSURLConnection *)connection { 
    NSString *responseText = [[NSString alloc] initWithData:signInData encoding:NSUTF8StringEncoding]; 

    NSLog(@"%@", @"check"); 
    NSLog(@"%@", responseText); 
} 

// an example response would be: 
// <string xmlns="http://thedomain.com/">invalid login</string> 
+0

xml 응답을 구문 분석하는 방법을 묻는 질문이 있으십니까? – cpjolicoeur

답변

2

롤링 볼을 얻을 수 있지만, 문자열 응답 "<string xmlns="http://thedomain.com/">invalid login</string>"나는 유효한 로그인이 같을 것이라고 믿고있어 같은 간단한 "<string xmlns="http://thedomain.com/">valid login</string>"

그건 당신이 단순히 문자열 @"valid login"을 포함하지만, @"invalid login"

012를 포함하지 않는 응답을 볼 수있는 경우 인 경우
if (([responseText rangeOfString:@"invalid login"].location == NSNotFound) && ([responseText rangeOfString:@"valid login"].location != NSNotFound)){ 
    // Congrats valid 
} 

성공적인 응답이 "<string xmlns="http://thedomain.com/">successful login</string>"이면 if 문을 쉽게 따라 할 수 있습니다.

+0

필자는 XML을로드하는 방법을 완전히 바꿨지 만이 솔루션은 "간단한"구문 분석에 적합합니다. – Jacksonkr