NSURL URLWithString: is null with non-english accented characters

regulus6633 picture regulus6633 · Jan 24, 2010 · Viewed 19.7k times · Source

I have the following string...

NSString *googleSearchString = @"http://www.google.com/search?q=lyrics+%22Tænder+På+Dig%22+%22Jakob+Sveistrup%22";

Notice that it has some accented characters. When I try to turn that into a url the returned url is null...

[NSURL URLWithString:googleSearchString];

So normally the url works except when there are accented non-english characters in the string. Any help on how to handle that?

Answer

clee picture clee · Jan 24, 2010

You need to escape the special characters to make it work properly. Something like:

[NSURL URLWithString:[googlSearchString stringByAddingPercentEscapesUsingEncoding: NSUTF8StringEncoding]];