简体   繁体   中英

iOS HTML parse issue

I have implemented HTMLParser to my project. I am connecting to a billing web site to my cell operator, and I need parse different variables.

From HTML content

I need get one tag that giving me an indication if the username and password were not OK and show an alert to users.

NSArray *spanNodes = [bodyNode findChildTags:@"div"];

for (HTMLNode *spanNode in spanNodes) {
    if ([[spanNode getAttributeNamed:@"class"] isEqualToString:@"notification-item notification-item-error"]) {
        //Above my tag
        [self alertMessageLogin];
        NSLog(@"%@", [spanNode rawContents]); //Answer to second question
    }
    else {
        NSlog(@"You are not logged in");
    }
}

But if I add else to this code, so I need check else no tag "notification-item notification-item-error", so show me another view.

In my case, after if , my code is going to else from some reason, so my code think that the if condition is false and going to the else condition. But without else all is working fine. Here is my log from else . I don't know, why is it printing a few "You are logged in"?

How do I fix this issue?

2013-04-09 23:48:50.828 GolanTelecom[90143:c07] You are logged in
2013-04-09 23:48:50.830 GolanTelecom[90143:c07] You are logged in
2013-04-09 23:48:50.830 GolanTelecom[90143:c07] You are logged in
2013-04-09 23:48:50.830 GolanTelecom[90143:c07] You are logged in
2013-04-09 23:48:50.830 GolanTelecom[90143:c07] You are logged in
2013-04-09 23:48:50.831 GolanTelecom[90143:c07] You are logged in
2013-04-09 23:48:50.831 GolanTelecom[90143:c07] You are logged in
2013-04-09 23:48:50.831 GolanTelecom[90143:c07] You are logged in
2013-04-09 23:48:50.832 GolanTelecom[90143:c07] You are logged in
2013-04-09 23:48:50.832 GolanTelecom[90143:c07] You are logged in
2013-04-09 23:48:50.832 GolanTelecom[90143:c07] You are logged in
2013-04-09 23:48:50.833 GolanTelecom[90143:c07] You are logged in
2013-04-09 23:48:50.833 GolanTelecom[90143:c07] You are logged in
2013-04-09 23:48:50.833 GolanTelecom[90143:c07] You are logged in
2013-04-09 23:48:50.833 GolanTelecom[90143:c07] You are logged in
2013-04-09 23:48:50.834 GolanTelecom[90143:c07] You are logged in
2013-04-09 23:48:50.834 GolanTelecom[90143:c07] <div
class="notification-item notification-item-error"><div
class="notification-item-inner"><span
class="notification-content">???? ???? ?? ?????
??????</span></div></div> 2013-04-09 23:48:50.834
GolanTelecom[90143:c07] You are logged in 2013-04-09 23:48:50.835
GolanTelecom[90143:c07] You are logged in 2013-04-09 23:48:50.835
GolanTelecom[90143:c07] You are logged in 2013-04-09 23:48:50.835
GolanTelecom[90143:c07] You are logged in

Here is the working code:

    NSError *error = nil;
    NSString *html = str;
    HTMLParser *parser = [[HTMLParser alloc] initWithString:html error:&error];

    if (error) {
        NSLog(@"Error: %@", error);
        return;
    }

    HTMLNode *bodyNode = [parser body];
    NSArray *spanNodes = [bodyNode findChildTags:@"div"];
    BOOL found = NO;

    for (HTMLNode *spanNode in spanNodes) {
        if ([[spanNode getAttributeNamed:@"class"] isEqualToString:@"notification-item notification-item-error"]) {
            [self alertMessageLogin];
            found = YES;
            NSLog(@"%@", [spanNode rawContents]);
            break;
        }
    }

    if ( NO == found) {
        //NSLog(@"Hey, I'm here!");
        [self HideLoginScreenWhenLogged];
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM