51

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?

skaffman
  • 398,947
  • 96
  • 818
  • 769
regulus6633
  • 18,848
  • 5
  • 41
  • 49

5 Answers5

73

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

[NSURL URLWithString:[googlSearchString stringByAddingPercentEscapesUsingEncoding: NSUTF8StringEncoding]];
clee
  • 10,943
  • 6
  • 36
  • 28
  • 1
    That works! Thanks for the answer. I had just found the function CFURLCreateStringByAddingPercentEscapes() also so either would do the job. – regulus6633 Jan 24 '10 at 00:08
  • 1
    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 Jan 24 '10 at 00:11
  • 1
    Question: for web urls... Should you use NSASCIIStringEncoding or NSUTF8StringEncoding – regulus6633 Jan 24 '10 at 00:23
  • I would stick with NSASCIIStringEncoding myself. – clee Jan 24 '10 at 00:27
  • 9
    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 Jan 24 '10 at 07:17
  • 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 Jan 24 '10 at 15:19
  • After testing on a friend's Danish machine, NSASCIIStringEncoding did not work. NSUTF8StringEncoding did work so I'm going with that. – regulus6633 Jan 25 '10 at 00:27
  • D'oh! My mistake, sorry for the bad advice. I'll edit the answer to reflect the correct encoding. – clee Jan 25 '10 at 02:37
  • 1
    In the example the OP has a string that is already percent escaped, won't this doubly escape the %22's making the url no longer point at the same place? – ToddH Jan 28 '13 at 17:58
11

Use this for SWIFT 4:

let url = myURLString.addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed)
let myURL = URL(string: url)
Yaroslav Dukal
  • 3,894
  • 29
  • 36
7

Using Swift 2.2

For escaping non-english characters, for example: to make an URL request do:

let urlPath = path.URLString.stringByAddingPercentEncodingWithAllowedCharacters(NSCharacterSet.URLQueryAllowedCharacterSet())

Here urlPath is an Optional and path is your original url (the one with non-english characters)

Hugo Alonso
  • 6,684
  • 2
  • 34
  • 65
1

In 2k16 method stringByAddingPercentEscapesUsingEncoding: is deprecated and there is no way escape this correctly. When URL is predefined just use browser encoded string, because stringByAddingPercentEncodingWithAllowedCharacters: method cannot escape whole URL.

Timur Bernikovich
  • 5,660
  • 4
  • 45
  • 58
-1

Some times a space in the url can cause this problem .

Martin Jacob
  • 356
  • 1
  • 7
  • 16