NSURL is null when the url string parameter has a space ..?

I am parsing xml through my aspx page in my iphone application. I do this to get the XML data from the url and add it to NSData, as shown below.

NSString *urlString = [NSString stringWithFormat:@"http://www.abc.com/parsexml.aspx?query=%@", searchBar.text]; NSURL *url = [NSURL URLWithString:urlString]; 
Error

occurs when my urlString has spaces between characters, e.g. (http://www.abc.com/parsexml.aspx?query=iphone 32gb 3gs)

Please help me. What should I do to solve this problem.

+6
source share
2 answers

Use NSString -(NSString *)stringByAddingPercentEscapesUsingEncoding:(NSStringEncoding)encoding; to encode url request data.

Check out the document here .

 NSString *urlString = [NSString stringWithFormat:@"http://www.abc.com/parsexml.aspx?query=%@", [searchBar.text stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]]; 
+18
source

You can use% 20 instead of space Remove space at% 20 in your URL string

Hope this helps you.

+2
source

Source: https://habr.com/ru/post/893584/


All Articles