I just build my app with WKWebView with swift 4.0 (iOS 12.1)
I need to run my javascript from online html, However if I move url from index to another page, my print message does not working at all.
let contentController = WKUserContentController()
contentController.add(self, name: "myHandler")
let configuration = WKWebViewConfiguration()
configuration.userContentController = contentController
webview = WKWebView(frame: self.view.frame, configuration: configuration)
webview.uiDelegate = self
webview.navigationDelegate = self
self.view = self.webview
override func viewDidLoad() {
super.viewDidLoad()
let url = URL(string: "example.com/index") // of course https://
let request = URLRequest(url: url!)
webview.load(request)
I missed userContentController when I move to another html page in the webView. Index page can call this one but another page do not call any function.
index.html(ok) -> link to a.html -> a.html (not ok) -> link to index.html -> index.html(ok)
Do not Call this one :
func userContentController(_ userContentController: WKUserContentController, didReceive message: WKScriptMessage) {
print("call print out")
if message.name == "myHandler" {
print("JS -> Native Call \(message.body)")
abc()
} else {
print("JS -> Native N/A")
}
}
I cannot get any message even "call print out" at all. How can I active userContentController in my code?