2010-12-13 62 views
0

我想解析HTML內容使用HTMLParser和它的幫助我試圖啓動UIAlertView,應用程序運行正常,但不啓動UIAlertView。UIAlertView沒有得到啓動

下面的代碼:

- (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]; 

} 

回答

0

你AlertViews似乎是適當的形成(儘管來回郵件,你不需要[的NSString stringWithFormat:]的東西,因爲你沒有實際格式化什麼 - 只是@「你的消息」的東西是好的)。

既然它們很好,那就告訴我們,使它們出現的條件從來不會發生。您的isEqualToString比較都不是true,或者errorNodes和spanNodes都是空的,或者是這些東西的某種組合。

點擊第一個聲明並設置一個斷點。構建和調試,讓程序運行直到達到斷點。現在您可以檢查並查看errorNodes和spanNodes實際包含的內容。

+0

謝謝,問題出在errorNode。返回的數據與它不匹配,因爲我使用了錯誤的標籤。 :P – 2010-12-13 12:47:57