iOS - Download an HTML web page, search through the content and set text to a label based on query result

StackOverflow https://stackoverflow.com/questions/9788343

문제

what I want:

Download a webpage from a server > Save content in a NSString > Search through the content via IsEqualToString > Set text to a label when a specified keyword was found.

I was able to download the webpage, show content in a UITextView but my if function always executes the else part doesn't matter what I'm searching for in IsEqualToString. Please help.

Actually I should see in my label: Course is open and NOT Sorry, course is closed. See screenshot.

//
//  ViewController.h
//  TestingHTML2
//
//  Created by Marc Woerner on 20.03.12.
//  Copyright (c) 2012 __MyCompanyName__. All rights reserved.
//

#import <UIKit/UIKit.h>

@interface ViewController : UIViewController
{
    NSMutableData *myData;
    NSURLConnection *myConnection;
}

@property (strong, nonatomic) IBOutlet UITextView *myTextView;
@property (strong, nonatomic) IBOutlet UILabel *myLabel;

@end

ViewController.m

//
//  ViewController.m
//  TestingHTML2
//
//  Created by Marc Woerner on 20.03.12.
//  Copyright (c) 2012 __MyCompanyName__. All rights reserved.
//

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController
@synthesize myTextView;
@synthesize myLabel;


- (void)viewDidLoad
{
    [super viewDidLoad];
    if (myConnection == nil) {
        myData = [NSMutableData new];
        NSString *urlString = [NSString stringWithFormat:@"http://www.golfplatz-altenstadt.de"];
        myConnection =[NSURLConnection connectionWithRequest:
                      [NSURLRequest requestWithURL:
                      [NSURL URLWithString:urlString]] 
                        delegate:self];
    }
}




- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
    [myData appendData:data];
}




- (void) initializeVariablesAgain {
    myData = nil;
    myConnection = nil; 
    myTextView = nil;
}




- (void)connectionDidFinishLoading:(NSURLConnection *)connection {

    NSString *stringToLookup = [[NSString alloc]initWithData:myData encoding:NSASCIIStringEncoding];

    if ([stringToLookup isEqualToString:@"platz_bespielbar"]) {
        myLabel.text = @"Course is open";
    } else {
        myLabel.text = @"Sorry, course is closed";
    }

    myTextView.text = [[NSString alloc] initWithData:myData encoding:NSASCIIStringEncoding];

    [self initializeVariablesAgain];
}

When I look through the downloaded web page myself I see an entry:

platz_bespielbar

So why does my function don't work?

enter image description here

도움이 되었습니까?

해결책

It's because you are checking the whole string and not only part of it.

if ([stringToLookup isEqualToString:@"platz_bespielbar"]) {

should be

if ([stringToLookup rangeOfString:@"platz_bespielbar"].location != NSNotFound]) {

Hope it helps

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top