0

I want the url of the webpage of whatever to look something like this:

foo.com/l=english&se=local&incEN=false&q=bob%20bob&

the problem here is that when i say

urlString = @"foo.com/l=english&se=local&incEN=false&q=bob%20bob&";
[self initWithURL:[NSURL URLWithString:urlString]];

I want the website url string to be "foo.com/l=english&se=local&incEN=false&q=bob%20bob&" but it is giving me:

foo.com/l=english&se=local&incEN=false&q=bob22520bob&

as the string for the NSURL.

How do I fix this so that I get the desired string to be set as the NSURL string?

Mike Abdullah
  • 14,933
  • 2
  • 50
  • 75
user2905147
  • 21
  • 2
  • 4

3 Answers3

2

Try this function:

NSString* urlString = @"foo.com/l=english&se=local&incEN=false&q=bob bob&"
NSString* urlEscaped [urlString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];

No need to add %20 in the URL, just escape it in the next function.

Sho
  • 294
  • 1
  • 5
  • No this is giving me: foo.com/l=english&se=local&incEN=false&q=bob2ob& which i do not want. i want the '%' sign to be there after nsurl – user2905147 Dec 15 '13 at 12:25
1

Try replacing %20 with +.
I often see spaces in URLs encoded as a plain + as well.

Christian Schnorr
  • 10,768
  • 8
  • 48
  • 83
0

Perhaps the url

urlString = @"foo.com/l=english&se=local&incEN=false&q=bob%20bob&";

is already using some kind of encoding for space as "%20",so using "stringByAddingPercentEscapesUsingEncoding" method will encode it again which will result as"%22520" for space.Also the encoding method may not work correctly if it is http not https.For me using https instead of http and encoding the url string with NSUTF8StringEncoding worked.

iSankha007
  • 385
  • 15
  • 21