Waqass Karim
Waqass Karim

Reputation: 71

NSURL value goes null when url string parameter has space..?

I am parsing xml through my aspx page to my iphone app. I am doing this way to get XML data from url and append into NSData like this below.

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

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

please help me. What should i do to resolve this issue.

Upvotes: 6

Views: 5426

Answers (2)

viral shah
viral shah

Reputation: 93

You Can Use %20 Instead of Space Remove Space By %20 in your URL String

Hope It Will Help You..

Upvotes: 2

user756245
user756245

Reputation:

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

Check the doc here.

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

Upvotes: 18

Related Questions