Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
504 views
in Technique[技术] by (71.8m points)

cocoa - NSURL URLWithString: is null with non-english accented characters

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...