NSURL URLWithString: Null with non-English accented characters

I have the following line ...

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

Please note that it has some accented characters. When I try to turn this into a url, the returned url is null ...

 [NSURL URLWithString:googleSearchString]; 

Thus, the URL usually works, unless there are characters with non-English letters in the string. Any help on how to handle this?

+45
cocoa special-characters nsurl
Jan 23
source share
4 answers

You need to avoid special characters so that they work correctly. Something like:

 [NSURL URLWithString:[googlSearchString stringByAddingPercentEscapesUsingEncoding: NSUTF8StringEncoding]]; 
+68
Jan 23 '10 at 23:44
source share

Using Swift 2.2

To escape non-English characters, for example: to request a URL:

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

Here urlPath is Optional , and path is your original url (one that has non-English characters)

+7
Apr 19 '16 at 19:59
source share

In 2k16 method, stringByAddingPercentEscapesUsingEncoding: deprecated, and this is not possible. When the URL is predefined, just use the browser encoded string because the stringByAddingPercentEncodingWithAllowedCharacters: method cannot escape the whole URL.

0
Dec 12 '16 at 15:16
source share

Sometimes this can cause a space in the url.

-one
Sep 10 '13 at 5:52
source share



All Articles