0

I'm parsing XML using CXML in my iphone app, works fine when the locaiton I'm searching for (using query string) is a single word. However when I add a space (Say i'm searching for shoe shop) it falls over. I tried replacing the " " space with a %20 but it doesn't seem to be able to read that url back when it parses.

My code:

- (IBAction)doSearch {

    NSString *trimmedWhat = [txtboxWhat.text stringByReplacingOccurrencesOfString:@" " withString:@"%20"];
    NSString *trimmedWhere = [txtboxWhere.text stringByReplacingOccurrencesOfString:@" " withString:@""];

    NSString *tempFullUrl = [NSString stringWithFormat:@"http://sampleurl.com/mobilesearch/place/%@/0/0/0/%@/0/0/0/0/0/0/0/0/search.aspx", trimmedWhat,  trimmedWhere];
    searchType = @"fullSearch";
    NSLog(@"Full String: %@",tempFullUrl);
    NSLog(@"Search Type: %@",searchType);

    PromotionViewController *passArray = [[PromotionViewController alloc] initWithNibName:@"PromotionViewController" bundle:nil];
    [passArray setCurrentCat: tempFullUrl];
    [passArray setCurrentType: searchType];     
    [self.navigationController pushViewController:passArray animated:YES];
    [PromotionViewController release];
}

Then on my PromotionViewController:

 NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:                                
    [NSURL URLWithString: [NSString stringWithFormat:[NSString stringWithFormat:@"%@", currentCat]]]];
    [request setHTTPMethod: @"GET"];
    dataReply = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
    stringReplyServer = [[NSString alloc] initWithData:dataReply encoding:NSUTF8StringEncoding];

    if(currentType == @"categorySearch") {
...do parse
}

It just seems to fall over when the returning url for place

How do I sanitise the URL?

Tom

EDIT

I've added the following to my original pass through of search

 NSString *utfString = [tempFullUrl UTF8String];

    PromotionViewController *passArray = [[PromotionViewController alloc] initWithNibName:@"PromotionViewController" bundle:nil];
    [passArray setCurrentCat: utfString];
    [passArray setCurrentType: searchType];     
    [self.navigationController pushViewController:passArray animated:YES];
    [PromotionViewController release];

however it falls over with the following:

2011-11-18 10:41:52.677 Del Search[2312:f203] Full String: http://web-xml.asdasdas.com/mobilesearch/place/(null)/0/0/0/<UITextField: 0x74709d0; frame = (15 7; 286 31); text = 'london'; clipsToBounds = YES; opaque = NO; autoresize = RM+BM; layer = <CALayer: 0x7470af0>>/0/0/0/0/0/0/0/0/search.aspx
MissCoder87
  • 2,669
  • 10
  • 47
  • 82

2 Answers2

1
- (IBAction)doSearch {
NSString * trimmedWhat =  [txtboxWhat.text stringByTrimmingCharactersInSet:        [NSCharacterSet whitespaceAndNewlineCharacterSet]];
NSString * trimmedWhere =  [txtboxWhat.text stringByTrimmingCharactersInSet:        [NSCharacterSet whitespaceAndNewlineCharacterSet]]; 
    NSString *tempFullUrl = [NSString stringWithFormat:@"http://sampleurl.com/mobilesearch/place/%@/0/0/0/%@/0/0/0/0/0/0/0/0/search.aspx", trimmedWhat,  trimmedWhere];
    searchType = @"fullSearch";
    NSLog(@"Full String: %@",tempFullUrl);
    NSLog(@"Search Type: %@",searchType);

    PromotionViewController *passArray = [[PromotionViewController alloc] initWithNibName:@"PromotionViewController" bundle:nil];
    [passArray setCurrentCat: tempFullUrl];
    [passArray setCurrentType: searchType];     
    [self.navigationController pushViewController:passArray animated:YES];
    [PromotionViewController release];
}

NSString *utfString = [currentCat UTF8String];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:                                
    [NSURL URLWithString: utfString]];
    [request setHTTPMethod: @"GET"];
    dataReply = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
    stringReplyServer = [[NSString alloc] initWithData:dataReply encoding:NSUTF8StringEncoding];

    if(currentType == @"categorySearch") {
...do parse
}
Sudesh Kumar
  • 569
  • 3
  • 13
  • Thanks for that - i'm getting the following error on the line that sets the UTF8 formatting: http://content.screencast.com/users/TomBeech/folders/Jing/media/7f77284f-02f6-488b-92ad-00ea4ef262de/00000011.png (this line NSString *utfString = [currentCat UTF8String];) – MissCoder87 Nov 18 '11 at 11:49
  • NSString *utfString = (NSString *)[currentCat UTF8String]; use this one – Sudesh Kumar Nov 18 '11 at 12:05
  • Really appreciate all your comments matey, falls over with the same error :( – MissCoder87 Nov 18 '11 at 13:04
  • actually I lie, it falls over, but not that error, just a exc_bad_access error on the [NSURL URLWithString: utfString]]; lie – MissCoder87 Nov 18 '11 at 13:06
  • I worked it out! NSString *utfString = [currentCat stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]; – MissCoder87 Nov 18 '11 at 13:29
1

To fix it I just did:

NSString *utfString = [currentCat stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
MissCoder87
  • 2,669
  • 10
  • 47
  • 82