Execute JavaScript methods from native code
iOS
Android
You can execute JavaScript in an in-app message from native code by completing the following steps:

Implement and assign a MessagingDelegate

To register a JavaScript event handler with a Message object, you will first need to implement and set a MessagingDelegate.
For more detailed instructions on implementing and using a MessagingDelegate, please read the tutorial on using MessagingDelegate.

Obtain a reference to the WKWebView

In the shouldShowMessage function of the MessagingDelegate, get a reference to the WKWebView used by the message.
Swift
1
func shouldShowMessage(message: Showable) -> Bool {
2
// access to the whole message from the parent
3
let fullscreenMessage = message as? FullscreenMessage
4
let message = fullscreenMessage?.parent
5
6
let messageWebView = message?.view as? WKWebView
7
8
...
9
}
Copied!

Call the JavaScript method

With a reference to the WKWebView, the instance method evaluateJavaScript(_:completionHandler:) can now be leveraged to call a JavaScript method.
Further details of this API are explained in the Apple documentation - the example below is provided for the purpose of demonstration:
Swift
1
func shouldShowMessage(message: Showable) -> Bool {
2
// access to the whole message from the parent
3
let fullscreenMessage = message as? FullscreenMessage
4
let message = fullscreenMessage?.parent
5
6
let messageWebView = message?.view as? WKWebView
7
messageWebView?.evaluateJavaScript("startTimer();") { result, error in
8
if error != nil {
9
// handle error
10
return
11
}
12
13
if result != nil {
14
// do something with the result
15
}
16
}
17
18
...
19
}
Copied!
You can execute JavaScript in an in-app message from native code by completing the following steps:

Implement and assign a MessagingDelegate

To register a JavaScript event handler with a Message object, you will first need to implement and set a MessagingDelegate.
For more detailed instructions on implementing and using a MessagingDelegate, please read the tutorial on using MessagingDelegate.

Obtain a reference to the WebView

In the shouldShowMessage function of the MessagingDelegate, get a reference to the WebView created for Javascript interactions.
Java
1
@Override
2
public boolean shouldShowMessage(FullscreenMessage fullscreenMessage) {
3
// access to the whole message from the parent
4
Message message = (Message) fullscreenMessage.getParent();
5
6
WebView webView = message.view;
7
8
...
9
}
Copied!

Call the JavaScript method

With a reference to the WebView, the instance method public void evaluateJavascript(@NonNull String script, @Nullable ValueCallback<String> resultCallback) can now be leveraged to call a JavaScript method.
Further details of this API are explained in the Android documentation - the example below is provided for the purpose of demonstration:
Java
1
@Override
2
public boolean shouldShowMessage(FullscreenMessage fullscreenMessage) {
3
// access to the whole message from the parent
4
Message message = (Message) fullscreenMessage.getParent();
5
6
WebView webView = message.view;
7
// webview operations must be run on the ui thread
8
webView.post(new Runnable() {
9
@Override
10
public void run() {
11
webView.evaluateJavascript("startTimer()", new ValueCallback<String>() {
12
@Override
13
public void onReceiveValue(String s) {
14
// do something with the content
15
}
16
});
17
}
18
});
19
20
...
21
}
Copied!
Export as PDF
Copy link
Edit on GitHub