EasyJSBridge makes JS in Android/iOS WebView callback interface unified, easier to call;

JS calls Demo

<script type="text/javascript" src="EasyJSBridge.js"></script>
<script type="text/javascript">
    var methods = ["test1", "test2", "test3"];
    var easyJSBridge = EasyJSBridge.create("android", "ios", methods);
    $(".test1").click(function() {
        easyJSBridge.test1("parameter1")
    });
    $(".test2").click(function() {
        easyJSBridge.test2("parameter1", 2)
    });
    $(".test3").click(function() {
        easyJSBridge.test3("androidParameter1", 2, ["iosParameter1", 2, "3"])
    });
</script>

Copy the code

convention

  • Through in the Android webView. AddJavascriptInterface (obj, ‘Android’) binding strong-arm;

  • In iOS, WKWebView WKScriptMessageHandler can be bound in either of two ways:

    1. Add a name with addScriptMessageHandler, and then resolve the method and parameters to be called with the agreed parameters

      [userContentController addScriptMessageHandler:self name:@"iOS"]; 
      
      Copy the code

      Then parse the data from the JS call:

      window.webkit.messageHandlers.iOS.postMessage({method:'test1',parameter:['','']});
      window.webkit.messageHandlers.iOS.postMessage({method:'test2',parameter:['','']})
      Copy the code

      {method:’test1′,parameter:[”,”]} {method:’test1′,parameter:[”,’]}

    2. Add multiple names with addScriptMessageHandler, and then differentiate the methods called based on the names

      [userContentController addScriptMessageHandler:self name:@"test1"];
      [userContentController addScriptMessageHandler:self name:@"test2"]; 
      
      Copy the code
      
      if ([message.name isEqualToString:@"test1"]) {
      
      
      } else if ([message.name isEqualToString:@"test2"]) {
      
      }
      Copy the code

      Then parse the data from the JS call:

      window.webkit.messageHandlers.test1.postMessage('arg1',2)
      window.webkit.messageHandlers.test2.postMessage('arg1','arg2')
      Copy the code
  • In a JS call, the convention’s callback method names are explicitly initialized in an array

      var methods = ["test1", "test2", "test3"];
      var easyJSBridge = EasyJSBridge.create("android", "ios", methods);
    Copy the code

API

Initialize the

var easyJSBridge = EasyJSBridge.create(‘androidObj’,’iOSObj’,[methodList])

Parameter names Will choose type instructions
androidObj is string The name of the object bound to Android WebView
iOSObj no string Name of the object bound to iOS WebView
methodList is An array of List of method names

note

  • If there is an iOSObj, then the countertone is agreed as follows
   window.webkit.messageHandlers.iOS.postMessage({method:'test1',parameter:['','']});
   window.webkit.messageHandlers.iOS.postMessage({method:'test2',parameter:['','']})
Copy the code
  • If there is no iOSObj, then the countertone is agreed as follows
   window.webkit.messageHandlers.test1.postMessage('arg1',2)
   window.webkit.messageHandlers.test2.postMessage('arg1','arg2')
Copy the code

Call the notorious

To initialize the

var methodList = ['method1','method2'];
var easyJSBridge = EasyJSBridge.create('androidObj','iOSObj',methodList);

Copy the code

It is then called directly based on the name of the methodList method at initialization

easyJSBridge.method1("arg1","arg2");
easyJSBridge.method2("arg1","arg2");
Copy the code

note

If Android and iOS call parameters have different values or different parameters, you can call them as follows:

easyJSBridge.method1("androidArg1","androidArg2",["iOSArg1","iOSArg2","iOSArg3"]);
Copy the code

The array parameters in method1 are for iOS, and the preceding parameters are for Android

License

MIT License

Copyright (c) 2018 Yale

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
Copy the code