我用 Swift 3 制作应用程序 enter image description here重定向网页 View 后首先登录我的应用程序。如果我单击网页 View 页面中的注销按钮,我想重定向回我的应用程序。我该怎么做?
最佳答案
您可以使用 WKScriptMessage
和 javascript 回调您的 viewController 并执行 native 代码。
在你的 View Controller 中:
//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>
关于ios - Swift 中的 Webview 管理,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42960137/