Webview manage in Swift

时间:2017-03-22 18:41:06

标签: ios iphone swift

I making app in Swift 3 enter image description hereFirstly login on my app after redirect web view.If i click logout button in web view page I want to redirect back my App.How can i do it ?

1 个答案:

答案 0 :(得分:0)

您可以使用WKScriptMessage和javascript对viewController进行回调并提取本机代码。

在你的viewController中:

//MARK: WKScriptMessageHandler (callback from js in HTML file)
func userContentController(_ userContentController: WKUserContentController, didReceive message: WKScriptMessage){
    if message.name == "callbackHandler" {
        print(message.body)

        //My function
        if (message.body as AnyObject).contains("myFunctionName"){
            self.playVideo()
        }
    }
}

在您的HTML文件中:

<!-- JS for Native Application Callbacks -->
<script type="text/javascript">
    function callNativeApp () {
        //iOS
        try {
            window.webkit.messageHandlers.callbackHandler.postMessage("myFunctionName");
        } catch(err) {
            console.log('The native context does not exist yet');
        }
    }
</script>