DSBridge is the easiest cross-platform (IOS/Android) javascript bridge on the planet. Since the 1.0 release, there has been a lot of feedback and support, and a 2.0 update to make DSBridge easier to use and more powerful.

The project address

Github.com/wendux/DSBr… (Android)

Github.com/wendux/DSBr… (IOS)

Update record

  1. Discard getJsBridge method and provide built-in object dsBridge;

    The steps for calling Native methods in 1.0 are as follows:

    var bridge = getJsBridge();/ / for bridge
    var str=bridge.call("testSyn", {msg: "testSyn"}); // Call native methods via bridge objectsCopy the code

    In 2.0 you can use dsBridge objects directly in one step:

    var str=dsBridge.call("testSyn", {msg: "testSyn"});Copy the code
  2. Support multiple returns

    Imagine a scenario where Native provides the ability to download files, and JS needs to display the download progress to the web page when calling native download module.

    The implementation in 1.0 is roughly as follows:

    var bridge = getJsBridge();/ / for bridge
    // Start the download
    var str=bridge.call("download", {url: "xxx"},function(result){
      // Download complete
    });
    
    // Register a global function that is called by callHandler whenever native has a new progress message
    window.onDownloadProgress=function(progress){
      // Update the display progress
    }Copy the code

    As you can see, the only way to handle the download progress is to register a JS callback, and then call the JS callback repeatedly through Native when the progress is updated. This method is obviously a bit complicated, and the elegant processing method should be like the following:

    var str=dsBridge.call("download", {url: "xxx"},function(result){
      if(result.status==0) {// Download complete
      }else{
        setProgress(result.progress) // Not finished, update progress}});Copy the code

    Yes, you can do that in 2.0! Natvie is required to simply cooperate (return data multiple times). For specific examples, please refer to the example of “multiple callbacks” callProgress in demo or click on this DSBridge example – show Native progress in web page

  3. JS API registration standardization

    In 1.0, JS functions for Native invocation only need to be declared as global functions without registration; However, in 2.0, to reduce the need to avoid JS global naming conflicts and to make JS API registrations more explicit, we provide a ritualistic register interface:

    JS defines an addition function for Native to call:

     dsBridge.register('addValue'.function(r,l){
             return r+l;
     })Copy the code

use

Use method see Github project home page, source code with examples for developers reference.

The last

If you like, welcome to Github star, thank you for your support! Github.com/wendux/DSBr… Github.com/wendux/DSBr…