2010-12-08 3 views
1

앱을 만들고 있습니다. 나는 HTTP POST 메쏘드를 사용하여 로그인 정보를 보내고있다. 서버로부터받는 replyI'm은 HTML 형식이다. 해당 HTML을 구문 분석하고 연속 또는 실패에 대해 다른 메소드를 추가하려면 어떻게해야합니까? 내가 달성하고자하는 것은 로그인 실패시 UIAlerView를 사용하여 메시지를 표시하고 로그인에 성공하면 앱이 애니메이션으로보기를 변경해야한다는 것입니다. :)HTML 응답 구문 분석 - iPhone App

내가 지금 사용하고 코드 : I는 내가 HTMLparser 클래스를 사용 정확히 했는가

- (IBAction) loginButton: (id) sender { 
indicator.hidden = NO; 
[indicator startAnimating]; 
loginbutton.enabled = NO; 

// Create the username and password string. 
// username and password are the username and password to login with 
NSString *postString = [[NSString alloc] initWithFormat:@"username=%@&password=%@",userName, password]; 
// Package the string in an NSData object 
NSData *requestData = [postString dataUsingEncoding:NSASCIIStringEncoding]; 

// Create the URL request 
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL: [NSURL URLWithString:@"http://localhost/dologin.php"]]; // create the URL request 
[request setHTTPMethod: @"POST"]; // you're sending POST data 
[request setHTTPBody: requestData]; // apply the post data to be sent 

// Call the URL 
NSURLResponse *response; // holds the response from the server 
NSError *error; // holds any errors 
NSData *returnData = [NSURLConnection sendSynchronousRequest: request returningResponse:&response error:&error]; // call the URL 

/* If the response from the server is a web page, dataReturned will hold the string of the HTML returned. */ 
NSString *dataReturned = [[NSString alloc] initWithData:returnData encoding:NSASCIIStringEncoding]; 

alertWithOkButton = [[UIAlertView alloc] initWithTitle:@"Status..." message:[NSString stringWithFormat:@"%@",dataReturned] delegate:self cancelButtonTitle:@"Okay" otherButtonTitles:nil]; 
[alertWithOkButton show]; 
[alertWithOkButton release]; 
} 
+0

넓은 질문입니다. 우선 HTML 구문 분석은 아마 여기에있는 방법이 아닙니다. SOAP 형식의 것이 바람직 할 수 있습니다. HTML의 모호한 부분에서 로그인을 분리하는 것이 좋을 것이므로, 할 수 있다면 그렇게하는 것이 좋습니다. 둘째, 노출 된 암호를 게시하는 것은 HTTPS를 사용하지 않는 한 나쁜 생각입니다. 셋째,이 질문을보십시오 : http://stackoverflow.com/questions/405749/parsing-html-on-the-iphone – Robert

+0

로버트에게 고마워, 내가 이것을 선택할 수 있도록 대답으로 올리면 어떨까. :) –

답변

0

. 이 클래스는 HTML 형식의 응답을받는 경우 매우 유용합니다.

0
-(void)startParsingForLogin:(NSString *)userIdStr Password:(NSString *)passwordStr 
{ 

NSString *urlString = [NSString stringWithFormat:@"http://www.example.com/loginxml.php?username=%@&password=%@",userIdStr,passwordStr]; 
////////NSLog(@"urlString : %@",urlString); 
NSURL *xmlURL = [NSURL URLWithString:urlString]; 

NSMutableURLRequest *request = [[[NSMutableURLRequest alloc] initWithURL:xmlURL cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:30.0]autorelease]; 

NSURLResponse *returnedResponse = nil; 
NSError *returnedError = nil; 
NSData *itemData = [NSURLConnection sendSynchronousRequest:request returningResponse:&returnedResponse error:&returnedError]; 
//NSString *itemString = [[[NSString alloc] initWithBytes:[itemData bytes] length:[itemData length] encoding:NSUTF8StringEncoding]autorelease]; 

//////NSLog(@"itemString : %@",itemString); 


xmlParser = [[NSXMLParser alloc] initWithData:itemData];   
[xmlParser setDelegate:self]; 

[xmlParser parse]; 

} 
- (void)parserDidStartDocument:(NSXMLParser *)parser 
{ 
////////NSLog(@"parserDidStartDocument"); 
} 

- (void)parser:(NSXMLParser *)parser parseErrorOccurred:(NSError *)parseError 
{ 
////////NSLog(@"parseErrorOccurred"); 
NSString * errorString = [NSString stringWithFormat:@"Error (Error code %i)", [parseError code]]; 
UIAlertView * errorAlert = [[UIAlertView alloc] initWithTitle:@"Error loading data" message:errorString delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil]; 
[errorAlert show]; 
[errorAlert release]; 
} 

- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qualifiedName attributes: (NSDictionary *)attributeDict 
{ 
////NSLog(@"didStartElement"); 
////NSLog(@"elementName : %@",elementName); 
////NSLog(@"namespaceURI : %@",namespaceURI); 
////NSLog(@"qualifiedName : %@",qualifiedName); 
////NSLog(@"attributeDict : %@",attributeDict); 
[registerNewArr addObject:attributeDict]; 

} 
- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string 
{ 
////NSLog(@"foundCharacters"); 
} 

- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName 
{ 
/////NSLog(@"didEndElement"); 
} 
- (void)parserDidEndDocument:(NSXMLParser *)parser 
{ 
}