swift - WKWebView JS 交互

本文介紹WKWebView怎么與js交互,至于怎么用WKWebView這里就不介紹了

html代碼

<html>
    <meta charset="UTF-8">
    <head>
        <title>
            H5測(cè)試
        </title>
    </head>
    <body>
        <h1 align="center">標(biāo)題1(App調(diào)用Js使標(biāo)題變成紅色)</h1>
        <script>
             //js調(diào)用APP-傳單個(gè)參數(shù)
             function callNativeApp(){
                try{
                    webkit.messageHandlers.callbackHandle.postMessage("Hello World")
                }catch(error){
                    console.error('The native context not exist ')
                }
            }
            //js調(diào)用APP-傳多個(gè)參數(shù)
            function callNativeApp2(){
                try{
                    webkit.messageHandlers.callbackHandle2.postMessage({key: "Hello", value: "World"})
                }catch(error){
                    console.error('The native context not exist ')
                }
            }
            //APP調(diào)用js
            function changeHead(){
                document.querySelector('h1').style.color = "red"
            }
            </script>

            <button style="text-align:center;height:50px;width:200px;font-size:16px" onclick="callNativeApp()">調(diào)用APP(單個(gè)參數(shù))</button>
            
            <button style="text-align:center;height:50px;width:220px;font-size:16px" onclick="callNativeApp2()">調(diào)用APP(多個(gè)個(gè)參數(shù))</button>
    </body>
</html>

APP調(diào)JS

  • 代碼
//調(diào)用js
webView.evaluateJavaScript("changeHead()", completionHandler: {
            (any, error) in
            if (error != nil) {
                Log.error("\(error)")
            }
        })
  • 結(jié)果
result1.png

JS給APP傳參數(shù)

    1. 首先注冊(cè)你需要監(jiān)聽的js方法名
//監(jiān)聽js
webView.configuration.userContentController.add(self, name: "callbackHandle")
webView.configuration.userContentController.add(self, name: "callbackHandle2")
  • 2.繼承WKScriptMessageHandler 并重寫userContentController方法,在該方法里接收J(rèn)S傳來的參數(shù)
@available(iOS 8.0, *)
func userContentController(_ userContentController: WKUserContentController,
                               didReceive message: WKScriptMessage) {

        switch message.name {
        case "callbackHandle":
            //單個(gè)參數(shù)
            Log.info("\(message.body)")
        case "callbackHandle2":
            //多個(gè)參數(shù)
            if let dic = message.body as? NSDictionary {
                let key: String = (dic["key"] as AnyObject).description
                let value: String = (dic["value"] as AnyObject).description

                Log.info("key: \(key)")
                Log.info("value: \(value)")

            }
        default: break
        }

    }
  • 3.結(jié)果
result.png
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容