views:

63

answers:

2

Hi All,

So I am trying to retrieve data from an XML stream coming from a URL. This URL is configured on a search string the user inputs. Is there any reason why this code should not be working?

NSString *searchString = "Geoff";

NSString *updatedURL = [NSString stringWithFormat:@"http://mysearchpage.com/searchQuery=%@", searchString];

NSLog(updatedURL);
NSURL *url = [[NSURL alloc] initWithString:updatedURL];

Now this works fine for single word searches, but as soon as I try and search for like a first and last name, the URL returns nil every time. Is there any behavior with the strings that may be causing that?

I have even tried to replace the " "'s with "%20"'s when the search string was appended to see if that was the problem. I did that using:

NSURL *url = [[NSURL alloc] initWithString:[updatedURL stringByReplacingOccurrencesOfString:@" " withString:@"%%20"]];

any ideas? Thanks in advance!

+4  A: 

You should use NSString's -stringByAddingPercentEscapesUsingEncoding: method for that:

NSURL *url = [[NSURL alloc] initWithString:
   [updatedURL stringByAddingPercentEscapesUsingEncoding: NSUTF8StringEncoding]];
Vladimir
That did the trick. Thanks for the quick reply!
gabaum10
A: 

If this is indeed a copy & paste of your code, it might be because the searchString is missing the @-sign. This should be NSString *searchString = @"Geoff";

Piro
No it's not a copy and paste, I simplified the URL so it would be easier to understand. I realize that is missing a bunch of stuff.
gabaum10