2

I am working on app, in which I interacting with web-service most of time. I am getting problem when I added space on somewhere it convert space to %20 when converting url to request. I googled but couldn't find any healthy solution according to my requirement because its only changing when url to request. Here is what I am doing.

    url = [url stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
  request = [NSURLRequest requestWithURL:[NSURL URLWithString:url]];
NSLog(@"Request is : %@", request);
[[NSURLConnection alloc] initWithRequest:request delegate:self];

and on NSLog it show me %20 where ever space is given. If someone know kindly suggest me better way to solve this issue. This will be great for me. Thanks in advance.

Mike Abdullah
  • 14,933
  • 2
  • 50
  • 75
josh
  • 1,681
  • 4
  • 28
  • 61
  • why do u want to remove it? it called url encoding, its required for url requests/ interacting with web services.. do u want to remove this %20 from the response? – vishy Apr 25 '13 at 09:00
  • It's question is duplicate of http://stackoverflow.com/questions/8107303/file-name-nsstring-adds-unnecessary-20-in-space – h4cky Apr 25 '13 at 09:08
  • @vishy because I am getting same response from webservice with "%20", so how can I deal with it? – josh Apr 25 '13 at 10:02
  • @josh i think u got the solution now.. – vishy Apr 25 '13 at 10:29

4 Answers4

5

You are actually converting spaces to %20 with this instruction:

url = [url stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]

But this is necessary in order to get the correct NSURLRequest, as some characters need this conversion.

You can reconvert them to "normal" characters using this other NSString method:

– stringByReplacingPercentEscapesUsingEncoding

Paras Joshi
  • 20,427
  • 11
  • 57
  • 70
Ricard Pérez del Campo
  • 2,387
  • 1
  • 19
  • 22
3

To Remove %20 from URL

url = [strFileName stringByRemovingPercentEncoding];
Hardik Thakkar
  • 15,269
  • 2
  • 94
  • 81
2

If you don't want %20 instead of space then why are you using stringByAddingPercentEscapesUsingEncoding?

stringByAddingPercentEscapesUsingEncoding - converts space to %20

Paras Joshi
  • 20,427
  • 11
  • 57
  • 70
Rinat
  • 598
  • 1
  • 8
  • 18
1

I solved this by using following line after getting response from web service.

 responseString= [responseString stringByReplacingOccurrencesOfString:@"%20" withString:@" "];

Now I getting correct data but I think this not a good solution :). Thanks for you answer posts.

josh
  • 1,681
  • 4
  • 28
  • 61