NSURL 和俄罗斯域名

NSURL and russian domains

这是个问题,我找不到解决方案。在我的程序中,我尝试使用 url 打开 Safari,直到出现这种情况才可以:我尝试 link 使用俄罗斯主机和域的站点,例如:

let url = "http://карта.рф"
UIApplication.sharedApplication().openURL(NSURL(string: url)!)

然后我有一个错误:

fatal error: unexpectedly found nil while unwrapping an Optional value

因此 class NSURL 无法在具有俄语符号的 link 上创建对象。我尝试使用 url.stringByAddingPercentEncodingWithAllowedCharacters(NSCharacterSet.URLFragmentAllowedCharacterSet())! 和类似的,但问题是它将所有 "incorrect" 符号更改为其他符号,但是在俄罗斯域中 link 完全是俄罗斯站点是必要的,这意味着 url-address.

中没有真正不正确的字母

我不认为 N​​SURL 不能使用非英文符号。谁能帮我?

您需要主机

相关的字符集
let string = "http://" + "карта.рф".stringByAddingPercentEncodingWithAllowedCharacters(.URLHostAllowedCharacterSet())!
let url = NSURL(string:string)

如果您还必须对 pathquery 进行编码,请使用 NSURLComponents 并单独对其他组件进行编码。

let components = NSURLComponents()
components.scheme = "http"
components.host = "карта.рф".stringByAddingPercentEncodingWithAllowedCharacters(.URLHostAllowedCharacterSet())

let url = components.URL!