[英]How to call functions in android and iOS swift from javascript in similar way
I am trying to call functions in android
and iOS Swift
(WKWebview)from javascript
. 我试图从javascript
调用android
和iOS Swift
(WKWebview)中的函数。
For Android: 对于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: JavaScript调用Android:
window.appInterface.postMessage("Hello);
Javascript call for iOS: Javascript调用iOS:
window.webkit.messageHandlers.appInterafce.postMeesage("Hello");
So, here we are making different calls from javascript
for android
and iOS
. 所以,这里我们正在为android
和iOS
进行不同的javascript
调用。 Is it possible to call functions in both android
and iOS
in one single way? 是否有可能以单一方式在android
和iOS
中调用函数?
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)
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.