I am trying to call functions in android
and iOS Swift
(WKWebview)from javascript
.
For Android:
public class WebAppInterface {
WebAppInterface(Context c) {
mContext = c;
}
@JavascriptInterface
public void postMessage(String message) {
Log.v(TAG, "message ----"+message);
Toast.makeText(mContext, message, Toast.LENGTH_SHORT).show();
}
}
webView.addJavascriptInterface(new WebAppInterface(mContext), "appInterface");
JavaScript call For Android:
window.appInterface.postMessage("Hello);
Javascript call for iOS:
window.webkit.messageHandlers.appInterafce.postMeesage("Hello");
So, here we are making different calls from javascript
for android
and iOS
.
Is it possible to call functions in both android
and iOS
in one single way?
Thanks.
Wrap them both up in a single function?
function postAppMessage(msg) {
if (window.webkit != undefined) {
if (window.webkit.messageHandlers.appInterface != undefined) {
window.webkit.messageHandlers.appInterface.postMessage(msg)
}
}
if (window.appInterface != undefined) {
window.appInterface.postMessage(msg)
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With