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

Is it possible to call native function in html, just like Android's JavascriptInterface and JsBridge #59

Closed
YouCii opened this issue Mar 12, 2019 · 5 comments
Labels
enhancement New feature or request

Comments

@YouCii
Copy link
Contributor

YouCii commented Mar 12, 2019

There are many onClick events in html that require native responses.

Is it possible to implement that?

@pichillilorenzo
Copy link
Owner

Yes, sure!

@pichillilorenzo pichillilorenzo added the enhancement New feature or request label Mar 12, 2019
@YouCii
Copy link
Contributor Author

YouCii commented Mar 12, 2019

That is great, haha

@YouCii YouCii closed this as completed Mar 12, 2019
@pichillilorenzo
Copy link
Owner

Feature added now on the new version 1.1.0.
Inside an html file, you may want use this:

<script>
        // you need to wait and listen the JavaScript event flutterInAppBrowserPlatformReady
        window.addEventListener("flutterInAppBrowserPlatformReady", function(event) {
            console.log("ready");
            // then you can call the window.flutter_inappbrowser.callHandler() method.
            // callHandler() returns a Promise that you can use to get data from Dart side.
            window.flutter_inappbrowser.callHandler('handlerFoo').then(function(result) {
                console.log(result, typeof result);
                console.log(JSON.stringify(result), result.bar);
            });

            window.flutter_inappbrowser.callHandler('handlerFooWithArgs', 1, true, ['bar', 5], {foo: 'baz'}).then(function(result) {
                console.log(result, typeof result);
                console.log(JSON.stringify(result));
            });
        });
</script>

Instead, on the onLoadStop Dart event, you can use callHandler directly:

// Inject JavaScript that will receive data back from Flutter
webView.injectScriptCode("""
  window.flutter_inappbrowser.callHandler('test', 'Text from Javascript').then(function(result) {
    console.log(result);
  });
""");

@YouCii
Copy link
Contributor Author

YouCii commented Mar 15, 2019

Yes, This is a good way to call the Dart method. Thanks for informing this new feature.

And how about Dart calls the JS method?
As the #62 shows, i still have same problem when i use webViewController.addJavaScriptHandler(HANDLER_NAME, callback), and i don't know how to handle it.

Has this problem been solved in the new version?

@YouCii
Copy link
Contributor Author

YouCii commented Mar 22, 2019

Happy news! Happy news!

I successfully got the response from JS using the code you provided, even though it still report the error log.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants