Skip to content

whiskeyfei/DSBridge-Android

 
 

Repository files navigation

DSBridge

MIT Licence

DSBridge is currently the best Javascript bridge in the world , by which we can call functions synchronously and asynchronously between web and Native . Moreover, both android and ios are supported !

DSBridge-IOS:https://github.com/wendux/DSBridge-IOS

DSBridge-Android:https://github.com/wendux/DSBridge-Android

2.0更新列表:https://juejin.im/post/593fa055128fe1006aff700a

修复部分设备上注入不成功,导致android 调用js 失败 bug fixed issues:wendux#18

Download

  1. Add the JitPack repository to your build file

    allprojects {
      repositories {
       ...
       maven { url 'https://jitpack.io' }
      }
    }
  2. Add the dependency

    dependencies {
    	compile 'com.github.wendux:DSBridge-Android:2.0-SNAPSHOT'
    	//compile 'com.github.wendux:DSBridge-Android:master-SNAPSHOT'
    }

Usage

  1. Implement apis in Java

    public class JsApi{
        //for synchronous invocation
        @JavascriptInterface
        String testSyn(JSONObject jsonObject) throws JSONException {
            return jsonObject.getString("msg") + "[syn call]";
        }
        //for asynchronous invocation
        @JavascriptInterface
        void testAsyn(JSONObject jsonObject, CompletionHandler handler) throws JSONException {
            handler.complete(jsonObject.getString("msg")+" [asyn call]");
        }
    }

    For security reason, Java api must be with "@JavascriptInterface" annotation, For more detail about this topic, please google .

  2. Setup api class to DWebView instance.

    import wendu.dsbridge.DWebView
    ...
    DWebView dwebView= (DWebView) findViewById(R.id.dwebview);
    dwebView.setJavascriptInterface(new JsApi());
  3. Call Java api in Javascript, and declare a global javascript function for the following java invocation.

    //init dsbridge
    window._dsbridge&&_dsbridge.init();
    
    //Call synchronously 
    var str=dsBridge.call("testSyn", {msg: "testSyn"});
    //Call asynchronously
    dsBridge.call("testAsyn", {msg: "testAsyn"}, function (v) {
      alert(v);
    })
    
    //Register javascrit function for Native invocation
     dsBridge.register('addValue',function(l,r){
         return l+r;
     })
  4. Call Javascript function in java

    webView.callHandler("addValue",new Object[]{1,"hello"},new OnReturnValue(){
           @Override
           public void onValue(String retValue) {
              Log.d("jsbridge","call succeed,return value is "+retValue);
           }
    });

​ Notice: Be sure that calling javascript functions must at "PageFinished".

Javascript API introduction

dsBridge

"dsBridge" is a built-in object , it has two method "call" and "register";

bridge.call(method,[args,callback])

Call Java api synchronously and asynchronously。

method: Java method name

args: arguments with json object

callback(String returnValue):callback to handle the result. only asynchronous invocation required.

dsBridge.register(methodName,function)

Register javascript method for Native invocation.

methodName: javascript function name

function: javascript method body.

Notice

Java api signature

In order to be compatible with IOS and Android, we make the following convention on native api signature:

  1. The tye of return value must be String; if not need, just return null.
  2. The arguments passed by JSONObject, if the api doesn't need argument, you still need declare the jsonObject argument.

More about DWebview

In DWebview, the following functions will execute in main thread automatically, you need not to switch thread by yourself.

void loadUrl( String url) 
void loadUrl(final String url, Map<String, String> additionalHttpHeaders)
void evaluateJavascript(String script) 

alert/confirm/prompt dialog

For alert/confirm/prompt dialog, DSBridge has implemented them all by default, if you want to custom them, override the corresponding callback in WebChromeClient class.

Finally

If you like DSBridge, please star to let more people know it , Thank you 😄.

About

An android bridge for calling functions synchronously and asynchronously between JavaScript and Java in WebView

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • Java 90.8%
  • HTML 9.2%