1

I'm creating an app. I'm sending the login info using HTTP POST method and the replyI'm getting from server is in HTML format. How can I parse that HTML and add different methods for succession or failure? What I'm trying to achieve is, upon login failure it should show the message using UIAlerView and upon successful login, the app should change the view with animation. :)

The code I'm using right now:

- (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];
}
Aniruddh
  • 7,648
  • 1
  • 26
  • 43
  • That's a broad question. First of all, parsing HTML is probably not the way to go here - a SOAP-type thing would be preferable, if you can do it, since you'd be better off decoupling the login from the vagaries of the HTML. Second of all, POSTing the bare password is a bad idea unless you use HTTPS. Third of all, see this question: http://stackoverflow.com/questions/405749/parsing-html-on-the-iphone – Robert Dec 08 '10 at 08:17
  • Thanks Robert, why don't you post this as an answer so I can select it. :) – Aniruddh Dec 13 '10 at 18:16

2 Answers2

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
{
}
Community
  • 1
  • 1
GhostRider
  • 1,197
  • 10
  • 19
0

What I did exactly is I used HTMLparser class. This class is very useful if you're getting response in HTML format.

Aniruddh
  • 7,648
  • 1
  • 26
  • 43