如何在swift中将字符串转换为URL

时间:2018-01-03 10:20:37

标签: ios iphone swift xcode

我想将以下字符串转换为网址

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

我尝试了let urlToSend = URL(string: "www.mydomain.com/key=अक्षय")!,但它返回了nil。

我从textField获得了'षक्षय'关键字,它可以是任何本地语言。

它适用于英语但不能使用当地语言。

我使用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

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

5 个答案:

答案 0 :(得分:5)

将URL作为字符串使用时,您应该将它们编码为URL有效;对URL进行编码的最常见示例之一是“”(空格)将被编码为“%20”。

因此,在您的情况下,您的网址的编码值应为:

<head>
  <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.2/css/bootstrap.min.css">
</head>

<div class="container">
<div class="col-md-3">
  <form class="navbar-form" role="search" action="javascript:alert('Inner Function performed!');">
    <div class="input-group add-on">
      <input class="form-control" placeholder="Search" name="srch-term" id="srch-term" type="text">
      <div class="input-group-btn">
        <button class="btn btn-default" type="submit"><i class="glyphicon glyphicon-search"></i></button>
      </div>
    </div>
  </form>
  				
</div>
</div>

您注意到www.mydomain.com%2Fkey=%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”

将使URL有效。

如何:

你可以得到上面这样的结果:

key

请注意,为了安全起见,编码字符串和网址都有可选绑定

解码网址

您还可以返回原始未编码的网址(解码),如下所示:

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
}

同样,可选的绑定是安全的。

答案 1 :(得分:4)

您需要对网址进行编码。

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

let decodedUrl = urlString?.removingPercentEncoding

请记住,您不应强制解包网址和字符串,如果是let或guard语句则使用。

答案 2 :(得分:2)

尝试这件事:

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

或者您可以使用:

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

答案 3 :(得分:1)

您可以使用appendingPathComponent逐个构建网址,而不是明确处理百分比编码:

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

答案 4 :(得分:0)

Swift3.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

用于检查目的

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