As referenced in the docs (turbo-ios/Advanced.md at main · hotwired/turbo-ios · GitHub) and in this blog post (Hybrid iOS apps with Turbo – Part 4: The JavaScript bridge | Masilotti.com), I’ve been trying to use session.webView.evaluateJavaScript()
to send a JS message to my Rails app.
However, unless I put the evaluateJavaScript()
call inside my sessionDidLoadWebView(_ session: Session)
method like so:
func sessionDidLoadWebView(_ session: Session) {
session.webView.navigationDelegate = self
let script = "document.body.style.background = 'orange';"
session.webView.evaluateJavaScript(script) { object, error in
if let error = error {
// handle error
} else if let object = object {
// success
}
}
}
…the JS doesn’t do anything.
I really need to be able to call evaluateJavascript
on demand, doing something like the following in a range of places, e.g after an a native button push, after an in-app purchase is complete, etc.:
let webView = session.webView
let script = "document.body.style.background = 'orange';" #in reality I'd call a JS method defined within my Rails app here using a JS bridge
webView.evaluateJavaScript(script) { object, error in
if let error = error {
// handle error
} else if let object = object {
// success
}
}
Has anybody got something like this working?
I’ve successfully got message handlers set up to receive Javascript messages from the Rails app but have had no luck sending messages the other way around.
Thank you!