Ios URL中的西里尔字母符号

Ios URL中的西里尔字母符号,ios,swift,nsurl,Ios,Swift,Nsurl,应用程序因以下url崩溃: let jsonUrl = "http://api.com/алматы/events" let session = NSURLSession.sharedSession() let shotsUrl = NSURL(string: jsonUrl) let task = session.dataTaskWithURL(shotsUrl!) 日志: 这是因为url中的西里尔字母符号。我怎样才能解决这个问题。谢谢你的帮助 URL不能包含西里尔字符。有一些标准如何将西里

应用程序因以下url崩溃:

let jsonUrl = "http://api.com/алматы/events"
let session = NSURLSession.sharedSession()
let shotsUrl = NSURL(string: jsonUrl)
let task = session.dataTaskWithURL(shotsUrl!)
日志:


这是因为url中的西里尔字母符号。我怎样才能解决这个问题。谢谢你的帮助

URL不能包含西里尔字符。有一些标准如何将西里尔字符翻译成有效的URL——如果搜索“Punicode”(P是故意的),您可能会发现一些东西

URL中需要转义非ASCII字符(以及许多特殊字符)。Chrome和其他浏览器会自动执行此操作。而且他们在地址栏中取消了URL的浏览,以获得更好的显示效果

因此,如果您有一个静态URL,只需将其粘贴到地址栏中,按enter键,再次选择URL,复制并粘贴到您的应用程序:

因此,不是:

let jsonUrl = "http://api.com/алматы/events"
您将获得:

let jsonUrl = "http://api.com/%D0%B0%D0%BB%D0%BC%D0%B0%D1%82%D1%8B/events"
试试这个:

let encodedUrl = jsonUrl.stringByAddingPercentEncodingWithAllowedCharacters(URLQueryAllowedCharacterSet)
大概是这样的:

import Foundation

func url(scheme scheme: String, host: String, path: String) -> NSURL? {
    let components = NSURLComponents()
    components.scheme = scheme
    components.host = host.stringByAddingPercentEncodingWithAllowedCharacters(NSCharacterSet.URLHostAllowedCharacterSet())
    components.path = path.stringByAddingPercentEncodingWithAllowedCharacters(NSCharacterSet.URLPathAllowedCharacterSet())
    return components.URL
}

// evaluates to http://api.com/%25D0%25B0%25D0%25BB%25D0%25BC%25D0%25B0%25D1%2582%25D1%258B/events
url(scheme: "http", host: "api.com", path: "/алматы/events")
让apiHost=”http://api.com/"
让apiPath=“ааааааац/事件”
让escapedPath=apiPath.stringByAddingPercentEncodingWithAllowedCharacters(NSCharacterSet.URLHostAllowedCharacterSet())
让url=NSURL(字符串:“\(apiHost)\(escapedPath!)”)
显然,您应该做一些更聪明的事情,而不仅仅是强制展开
escapedPath

以Swift的维基百科页面为例:
https://ru.wikipedia.org/wiki/Swift_(代码>

变成:

https://ru.wikipedia.org/wiki/Swift_(%D1%8F%D0%B7%D1%8B%D0%BA_%D0%BF%D1%80%D0%BE%D0%B3%D1%80%D0%B0%D0%BC%D0%BC%D0%B8%D1%80%D0%BE%D0%B2%D0%B0%D0%BD%D0%B8%D1%8F)
当粘贴到浏览器中时,它会将您带到正确的页面(大多数浏览器会方便地为您呈现UFT-8字符)。

请尝试在NSString上定义。您可能会看到有人通过添加百分比EscapeSusingEncode:
来建议使用
StringByAddingPercenteScapeSusingEncode:
,但这种方法在iOS 9中不受欢迎

还有一些预定义的<代码> NStRealStaseS,如 URLHooDeDealEdvestus URLPATABASEDIFECTUSE> 。因此,如果您真的必须在代码中解析未经处理的URL(使用预处理的URL,如公认的答案中所述,通常是一个更好的主意),您可以编写如下帮助器方法:

import Foundation

func url(scheme scheme: String, host: String, path: String) -> NSURL? {
    let components = NSURLComponents()
    components.scheme = scheme
    components.host = host.stringByAddingPercentEncodingWithAllowedCharacters(NSCharacterSet.URLHostAllowedCharacterSet())
    components.path = path.stringByAddingPercentEncodingWithAllowedCharacters(NSCharacterSet.URLPathAllowedCharacterSet())
    return components.URL
}

// evaluates to http://api.com/%25D0%25B0%25D0%25BB%25D0%25BC%25D0%25B0%25D1%2582%25D1%258B/events
url(scheme: "http", host: "api.com", path: "/алматы/events")
请注意,上述文档提到

此方法旨在对URL组件或子组件字符串进行百分比编码,而不是对整个URL字符串进行百分比编码

这是因为根据,并非URL的所有部分都可以进行百分比编码(例如,xamarin中的
scheme
-http/https/等)

var uri = new Uri (url);
var nsurl = new NSUrl (uri.GetComponents (UriComponents.HttpRequestUrl, UriFormat.UriEscaped));
UIApplication.SharedApplication.OpenUrl (nsurl);

Swift 4
使用字符串扩展名 创建名为String+Extension.swift的swift文件并粘贴此代码

import UIKit
extension String{
    var encodeUrl : String
    {
        return self.addingPercentEncoding(withAllowedCharacters: NSCharacterSet.urlQueryAllowed)!
    }
    var decodeUrl : String
    {
        return self.removingPercentEncoding!
    }
}
并像这样使用:(根据问题取样):


看看这个例子:维基百科上没有惩罚性代码。根据
NSURL
文档,转义字符的百分比将被解释为UTF-8。“Punycode用于在应用程序中的国际化域名(IDNA)框架中对标签进行编码,以便这些域名可以用互联网域名系统中允许的ASCII字符集表示。”因此,您所说的仅适用于URL的域部分,而不适用于其他部分。@aaisataev我提供了一个基本的答案,应该足以让您从中找到答案。Tnx,这是非常有用的答案,非常欢迎。如果有任何其他要求,请发表评论。把链接发给我。我已经创建了一堆xcode扩展。。确保开发简单快速。
"http://api.com/алматы/events".encodeUrl