Swift WKWebView URL连接中包含中文,特殊字符的处理

使用Swift WKWebView 打开包含中文,特殊字符 # % * 等特殊字符的链接,会出现链接不能访问的情况(安卓端和pc可以正常访问)。

解决这个问题,需要将urlString进行一下处理。

        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)

你可能感兴趣的:(Swift WKWebView URL连接中包含中文,特殊字符的处理)