2010-12-13 53 views
0

J'essaye d'analyser le contenu HTML en utilisant HTMLParser et avec l'aide de celui-ci j'essaie de lancer UIAlertView, l'application fonctionne bien mais n'initie pas UIAlertView.UIAlertView n'est pas initié

Voici le 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]; 

} 

Répondre

0

Vos AlertViews semblent être bien formé (mais vient les messages que vous n'avez pas besoin [NSString stringWithFormat:] des choses depuis que vous formatez pas vraiment quoi que ce soit - juste le @ "votre message" est bien). Comme ils sont corrects, cela nous indique que les conditions qui les font apparaître n'apparaissent jamais réellement. Aucune de vos comparaisons isEqualToString n'est vraie ou les deux, errorNodes et spanNodes, sont vides, ou une combinaison de ces éléments.

Cliquez à côté de la première instruction pour et définissez un point d'arrêt. Construire et déboguer et laisser le programme s'exécuter jusqu'à ce qu'il atteigne le point d'arrêt. Vous pouvez maintenant vérifier et voir ce que errorNodes et spanNodes contiennent réellement.

+0

Merci, le problème était en erreurNode. Les données renvoyées ne correspondaient pas car j'ai utilisé une mauvaise balise. : P –