This is An Android
library for Java
and JavaScript
communication .
iOS repo OCTWebViewBridge
All is builded by webview.loadUrl()
, which means this repo is not an JavaScript
interpreter .
This bridge handle below problems between Js and java communication
@JavaScriptInterface
cannot injectwindow.mine.func
intoJavaScript
@JavaScriptInterface
cannot handle the callback fromJavaScript
see this article WebView JS Java communication
In Java create a handler, like below LogHandler
,and override functions.
public class LogHandler extends JavaHandlerNoCallback{
public LogHandler(){
}
@Keep
public void log(String args){
Log.e("js obj: " + getJsObjectName(),args);
}
public String getJsDefine(){
return defineJsObj("log");
}
@Override
public String getJsObjectName() {
return LogHandler.class.getSimpleName();
}
@Override
public String getProjectObjectName() {
return "nativeUtils";
}
@Override
public String getJavaHandlerName() {
return LogHandler.class.getName();
}
@Override
public String getDefaultNativeFunc() {
return "log";
}
In JavaScript just call it.
function callJava() {
window.nativeUtils.LogHandler.log("123")
}