Jsbrage--The interactive bridge between WebView and JS in the app

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.