I'm trying to parse HTML content using HTMLParser and with the help of it I'm trying to initiate UIAlertView, 开发者_C百科the application runs fine but doesn't initiates UIAlertView.
Here's the code:
- (IBAction) loginButton: (id) sender
{
// 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. */
HTMLParser * parser = [[HTMLParser alloc] initWithData:returnData error:&error];
HTMLNode * bodyNode = [parser body];
NSArray * errorNodes = [bodyNode findChildTags:@"errorbox"];
for (HTMLNode * errorNode in errorNodes) {
if ([[errorNode getAttributeNamed:@"div class"] isEqualToString:@"errorbox"]){
alertWithOkButton = [[UIAlertView alloc] initWithTitle:@"Status..." message:[NSString stringWithFormat:@"Invalid Access Info, try again"] delegate:self cancelButtonTitle:@"Okay" otherButtonTitles:nil];
[alertWithOkButton show];
[alertWithOkButton release];
//Login Failed
}
}
NSArray * spanNodes = [bodyNode findChildTags:@"clientarea.php?action=masspay"];
for (HTMLNode * spanNode in spanNodes) {
if ([[spanNode getAttributeNamed:@"action"] isEqualToString:@"clientarea.php?action=masspay"]){
alertWithOkButton = [[UIAlertView alloc] initWithTitle:@"Status..." message:[NSString stringWithFormat:@"Login Accepted, redirecting to the main app screen. :)"] delegate:self cancelButtonTitle:@"Okay" otherButtonTitles:@"Go",nil];
[alertWithOkButton show];
[alertWithOkButton release]; //Login Success
}
}
[parser release];
}
Your AlertViews seem to be properly formed (though fro the messages you don't need the [NSString stringWithFormat: ] stuff since you're not actually formatting anything -- just the @"your message" stuff is fine).
Since they're fine, that tells us that the conditions that would make them appear never actually occur. Either neither of your isEqualToString comparisons is true or both errorNodes and spanNodes are empty, or some combination of those things.
Click next to the first for statement and set a breakpoint. Build and Debug and let the program run until it reaches the breakpoint. Now you can check and see what errorNodes and spanNodes actually contain.
精彩评论