views:

769

answers:

1

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?

+2  A: 

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

[NSURL URLWithString:[googlSearchString stringByAddingPercentEscapesUsingEncoding: NSUTF8StringEncoding]];
clee
That works! Thanks for the answer. I had just found the function CFURLCreateStringByAddingPercentEscapes() also so either would do the job.
regulus6633
It's worth noting, this will definitely handle your foreign characters, but it will not properly handle non-letter-characters like + or /. NSString doesn't have any function built in that really does URL encoding properly, but this one works for extended characters.
clee
Question: for web urls...Should you use NSASCIIStringEncoding or NSUTF8StringEncoding
regulus6633
I would stick with NSASCIIStringEncoding myself.
clee
Use `NSUTF8StringEncoding`. The encoding determines which bytes the percent escapes will describe. **If you use ASCII, accented characters are not guaranteed to survive or, if they do, to be encoded in any specific encoding**, because those characters are not in ASCII. Moreover, if any of the characters aren't in whatever encoding Cocoa graces you with (such as anything in pretty much any Asian language), the method will still return `nil`.
Peter Hosey
Annoyingly the docs make no mention of which encoding you should generally use (and Peter's sound advise). I'd encourage people to file a radar on it.
Mike Abdullah
After testing on a friend's Danish machine, NSASCIIStringEncoding did not work. NSUTF8StringEncoding did work so I'm going with that.
regulus6633
D'oh! My mistake, sorry for the bad advice. I'll edit the answer to reflect the correct encoding.
clee