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?
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