SwiftUI - как вызвать evalJavascript из кнопки?
Я хочу вызвать evalJavaScript из кнопки SwiftUI. Пожалуйста, посмотрите мой код и посоветуйте. Спасибо.
ContentView.swift
HStack {
TextField("Write message", text: $message).textFieldStyle(RoundedBorderTextFieldStyle())
Button(action: {
onReceive(self.viewModel.valuePublisher.receive(on: RunLoop.main)) { value in
self.message = "valueGotFromIOS('\(value)');"
}
}) {
...
}
}.padding()
WebView.swift
func webView(_ webView: WKWebView, didFinish: WKNavigation!) {
valueSubscriber = parent.viewModel.valuePublisher.receive(on: RunLoop.main).sink(receiveValue: { value in
webView.evaluateJavaScript(value) { (response, error) in
if let error = error {
print("Error calling javascript:valueGotFromIOS()")
print(error.localizedDescription)
} else {
print("Called javascript:valueGotFromIOS()")
}
guard let value = response as? String else {
return
}
self.parent.viewModel.valuePublisher.send(value)
}
})
}