Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

JavascriptChannels is not supported? I want js to call flutter's method #370

Closed
zhou-zc opened this issue May 29, 2020 · 6 comments
Closed

Comments

@zhou-zc
Copy link

zhou-zc commented May 29, 2020

**Flutter version:1.17.0
Plugin version: 3.2.0

@pichillilorenzo
Copy link
Owner

pichillilorenzo commented May 29, 2020

Yes, there are! Support has been added from version 0.3.0.. They are explained in the README.md file: About the JavaScript handler.

You can use addJavaScriptHandler({@required String handlerName, @required JavaScriptHandlerCallback callback}) to add a JavaScript message handler callback that listen to post messages sent from JavaScript by the handler with name handlerName, and removeJavaScriptHandler({@required String handlerName}) to remove it.

Example Flutter side:

class Foo {
  String bar;
  String baz;

  Foo({this.bar, this.baz});

  Map<String, dynamic> toJson() {
    return {
      'bar': this.bar,
      'baz': this.baz
    };
  }
}

// ... your widget code ...

onWebViewCreated: (InAppWebViewController controller) {
  webView = controller;

  controller.addJavaScriptHandler(handlerName:'handlerFoo', callback: (args) {
    return Foo(bar: 'bar_value', baz: 'baz_value');
  });

  controller.addJavaScriptHandler(handlerName: 'handlerFooWithArgs', callback: (args) {
    print(args[0]);
    print(args[1]);
    print(args[2]);
    print(args[3]);
    print(args[4]);
  });

},

JavaScript side:

<!doctype html>
<html lang="en">
    <head>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
        <meta http-equiv="X-UA-Compatible" content="ie=edge">
        <title>InAppWebViewJavaScriptHandlerTest</title>
    </head>
    <body>
        <h1>InAppWebViewJavaScriptHandlerTest</h1>
        <script>
            // You need to listen the "flutterInAppWebViewPlatformReady", otherwise it could not work properly!
            // Another way is to make sure that the page has been loaded and the DOM is ready!
            window.addEventListener("flutterInAppWebViewPlatformReady", function(event) {
                
                // call Flutter JavaScript handler callback!
                window.flutter_inappwebview.callHandler('handlerFoo').then(function(result) {
                    // use the "result" coming from Flutter side and call the other Flutter JavaScript handler callback and send arguments!
                    window.flutter_inappwebview.callHandler('handlerFooWithArgs', 1, true, ['bar', 5], {foo: 'baz'}, result).then(function(result) { });
                });

            });
        </script>
    </body>
</html>

@rizrmd
Copy link

rizrmd commented Jun 14, 2020

I think, it should be noted on documentation that addJavaScriptHandler should be called at onWebViewCreated.

I tried putting addJavaScriptHandler at another event handler, it does not work.

also, flutter_inappwebview.callHandler is not a function. The one available are flutter_inappwebview._callHandler (notice the underscore).

@pichillilorenzo
Copy link
Owner

you can use addJavaScriptHandler wherever you need to use it. Using it in onWebViewCreated is just an example.

The important thing is that you need to add your javascript handlers before you use them inside your html-css-js side if you want intercept that calls.

So, addJavaScriptHandler is not restricted to be used only on onWebViewCreated.

also, flutter_inappwebview.callHandler is not a function. The one available are flutter_inappwebview._callHandler (notice the underscore).

Please, take a look at the "About the JavaScript handler" section.
You need to wait and listen for the flutterInAppWebViewPlatformReady javascript global event:

window.addEventListener("flutterInAppWebViewPlatformReady", function(event) {
     console.log("ready");
});

Also, flutter_inappwebview._callHandler (the one with underscore) is a kind of "private" API and shouldn't/can't be used to work with flutter side. Instead, you need to use flutter_inappwebview.callHandler.

@rizrmd
Copy link

rizrmd commented Jun 16, 2020

window.addEventListener("flutterInAppWebViewPlatformReady", function(event) {
     console.log(window.flutter_inappwebview.callHandler);
});

returns undefined in my case. fresh flutter project, latest inappwebview.

@pichillilorenzo
Copy link
Owner

Could you post your full code? Flutter and html+css+js code, thanks

Copy link

This thread has been automatically locked since there has not been any recent activity after it was closed. If you are still experiencing a similar issue, please open a new bug and a minimal reproduction of the issue.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Nov 10, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants