使用Swift WKWebView 打開包含中文饱须,特殊字符 # % * 等特殊字符的鏈接域醇,會(huì)出現(xiàn)鏈接不能訪問的情況(安卓端和pc可以正常訪問)。
解決這個(gè)問題蓉媳,需要將urlString進(jìn)行一下處理譬挚。
var urlString: String?
let webView = WKWebView(frame: CGRect(x: 0, y: 0, width: screenWidth, height: screenHeight - navigationHeight ), configuration: WKWebViewConfiguration())
let charSet = CharacterSet.urlQueryAllowed as NSCharacterSet
let mutSet = charSet.mutableCopy() as! NSMutableCharacterSet
mutSet.addCharacters(in: "#")
let encodingURL = urlString?.addingPercentEncoding(withAllowedCharacters: mutSet as CharacterSet)
let myURL = NSURL.init(string: encodingURL!)
if myURL == nil {
return
}
let myRequest = URLRequest(url: myURL! as URL)
webView.load(myRequest)