URLString内有汉字转码方法

项目中使用wkwebview加载url遇到url含有中文加载闪退的问题

let path = "https://www.baidu.com/s?wd=含有中文"
 // 返回查询URL组件中允许使用的字符的字符集。
let urlString = path.addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed)
let myURL = URL(string: urlString!)
let myRequest = URLRequest(url: myURL!)
webView.load(myRequest)

你可能感兴趣的:(URLString内有汉字转码方法)