Question

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!

Was it helpful?

Solution

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

NSURL *url = [[NSURL alloc] initWithString:
   [updatedURL stringByAddingPercentEscapesUsingEncoding: NSUTF8StringEncoding]];

OTHER TIPS

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";

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top