In the hybrid app in the Web page are displayed in the native WebView, JS and the original communication is supported by the webview provided by the way to achieve, and the implementation of the abstract encapsulated module, is jsbrage.
Android:
There are 2 ways to invoke JS code for Android:
1. Adoption WebView
of loadUrl(),目前项目中用的这种方式
Mwebview.loadurl ("file:///android_asset/javascript.html"); Mwebview.loadurl ("Javascript:calljs ()");
2. WebView
evaluateJavascript(),
after Android 4.4 is used
New Valuecallback<string>() { @Override void onreceivevalue (String value) { /// Here is the result of JS return });
There are 3 ways for JS to call Android code:
1. Through WebView
the addJavascriptInterface()
object mapping, before the Android 4.2 version has a security vulnerability issue
2. WebViewClient
shouldOverrideUrlLoading ()
method callback to intercept URL
3. Passed WebChromeClient
onJsAlert()
, onJsConfirm()
onJsPrompt()
Method callback block JS dialog box, alert()
confirm()
prompt()
message
Each has advantages and disadvantages, reference 64904691
iOS has not studied carefully:
How iOS calls the JS code:
1. The Loadurl method can
2. Get the Javascriptcontext in the WebView in the iOS code and get the context. Invoking the JS method directly
JS Call the iOS code method:
1. As with Android, by blocking URLs
2. Get the Javascriptcontext in the WebView in the iOS code and get the context. Injection method, for JS call
3. It seems that the JS method can be called directly
Jsbrage--The interactive bridge between WebView and JS in the app