简体   繁体   English

如何快速将字符串转换为URL

[英]How to convert string to URL in swift

I want to convert below String to URL 我想将String下面的内容转换为URL

www.mydomain.com/key=अक्षय

I tried let urlToSend = URL(string: "www.mydomain.com/key=अक्षय")! 我试着let urlToSend = URL(string: "www.mydomain.com/key=अक्षय")! but it returns nil. 但它返回nil。

I'm getting that 'अक्षय' keyword from textField, it could be in any local language. 我从textField获得了“अक्षय”关键字,它可以是任何本地语言。

It works fine with English but not working with local language. 它适用于英语,但不适用于本地语言。

I used let url = URL(string: "www.mydomain.com")?.appendingPathComponent("key=अक्षय") it gives www.mydomain.com/key=%E0%A4%85%E0%A4%95%E0%A5%8D%E0%A4%B7%E0%A4%AF 我用let url = URL(string: "www.mydomain.com")?.appendingPathComponent("key=अक्षय")它给出了www.mydomain.com/key=%E0%A4%85%E0%A4%95% E0%A5%8D%E0%A4%B7%E0%A4%AF

but now I want to www.mydomain.com/key=%E0%A4%85%E0%A4%95%E0%A5%8D%E0%A4%B7%E0%A4%AF to www.mydomain.com/key=अक्षय 但现在我想www.mydomain.com/key=%E0%A4%85%E0%A4%95%E0%A5%8D%E0%A4%B7%E0%A4%AFwww.mydomain.com/key=अक्षय

When working with URLs as strings, you should encode them to be valid as a URL; 在将URL作为字符串使用时,应将它们编码为URL作为有效的字符串。 One of the most popular examples of encoding the URL is that the " " (space) would be encoded as "%20". 编码URL的最流行的例子之一是“”(空格)将被编码为“%20”。

So in your case the encoded value of your url should be: 因此,在您的情况下,URL的编码值应为:

www.mydomain.com%2Fkey=%E0%A4%85%E0%A4%95%E0%A5%8D%E0%A4%B7%E0%A4%AF

As you noticed the value of the key is changed 如您所见, key的值已更改

from: "अक्षय" 来自:“अक्षय”

to:"%E0%A4%85%E0%A4%95%E0%A5%8D%E0%A4%B7%E0%A4%AF" 到:“%E0%A4%85%E0%A4%95%E0%A5%8D%E0%A4%B7%E0%A4%AF”

which will let the URL to be valid. 这将使URL有效。

How to: 如何:

you could get the above result like this: 您可以这样获得以上结果:

let string = "www.mydomain.com/key=अक्षय"

if let encodedString  = string.addingPercentEncoding(withAllowedCharacters: .urlHostAllowed), let url = URL(string: encodedString) {
    print(url) // www.mydomain.com%2Fkey=%E0%A4%85%E0%A4%95%E0%A5%8D%E0%A4%B7%E0%A4%AF
}

Note that there is optional binding for both the encoded string and the url for the purpose of being safe. 请注意,为安全起见,编码字符串和url都有可选的绑定

Decoding the URL: 解码网址:

You could also returns to the original unencoded url (decoding it) like this: 您还可以像这样返回原始的未编码网址(将其解码):

let decodedString = "www.mydomain.com%2Fkey=%E0%A4%85%E0%A4%95%E0%A5%8D%E0%A4%B7%E0%A4%AF"

if let unwrappedDecodedString = decodedString.removingPercentEncoding {
    print(unwrappedDecodedString) // www.mydomain.com/key=अक्षय
}

Again, optional binding to be safe. 同样,可选绑定是安全的。

You need to encode the URL. 您需要对URL进行编码。

let urlString = "www.mydomain.com/key=अक्षय".addingPercentEncoding(withAllowedCharacters: .urlPathAllowed)
let url = URL(string: urlString!)

let decodedUrl = urlString?.removingPercentEncoding

Keep in mind that you shouldn't force unwrap URL's and strings, use if let or guard statements. 请记住,您不应该强行打开URL和字符串,而应使用let或guard语句。

Try this thing: 试试这个东西:

    let strURL = "www.mydomain.com/key=अक्षय".addingPercentEncoding(withAllowedCharacters: .urlHostAllowed)
    let url = URL(string: strURL!)

Or you can use: 或者您可以使用:

let strURL = "www.mydomain.com/key=अक्षय".addingPercentEncoding(withAllowedCharacters: .urlPathAllowed)
let url = URL(string: strURL!)

Instead of dealing with percent encodings explicitly, you can also build the URL piece by piece, using appendingPathComponent : 除了显式处理百分比编码,您还可以使用appendingPathComponent构建URL:

let url = URL(string: "www.mydomain.com")?.appendingPathComponent("key=अक्षय")
// www.mydomain.com/key=%E0%A4%85%E0%A4%95%E0%A5%8D%E0%A4%B7%E0%A4%AF

Swift3.0 迅捷3.0

let baseUrl = "www.mydomain.com/key=अक्षय" // base url

let encodedUrl : String! = baseUrl.addingPercentEncoding(withAllowedCharacters: CharacterSet.urlQueryAllowed) // remove the spaces in the url string

let typeUrl = URL(string: encodedUrl)! // convert the string into url

print(typeUrl)  // www.mydomain.com/key=%E0%A4%85%E0%A4%95%E0%A5%8D%E0%A4%B7%E0%A4%AF

For Checking purpose 用于检查目的

if let unwrappedDecodedString = encodedUrl.removingPercentEncoding {
        print(decodedString) // www.mydomain.com/key=अक्षय
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM