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

Some problem with javascriptHanlder #64

Closed
yhdjy025 opened this issue Mar 14, 2019 · 8 comments
Closed

Some problem with javascriptHanlder #64

yhdjy025 opened this issue Mar 14, 2019 · 8 comments

Comments

@yhdjy025
Copy link

yhdjy025 commented Mar 14, 2019

My window.flutter_inappbrowser object have a method named _callHandler but not callHandler
and I call this method, _callHandler in my JS code, but the InAppWebViewController can not recive the call

My js code:

  console.log('----------------------_callHandler--------------')
            console.log(window.flutter_inappbrowser._callHandler)
            console.log('----------------------callHandler--------------')
            console.log(window.flutter_inappbrowser.callHandler)
            //call app
            try {
                window.flutter_inappbrowser._callHandler('message', 'hello');
            } catch (e) {
                console.log(e)
            }

My dart code

return InAppWebView(
      initialUrl: Constant.SERVER_ADDRESS +
          'extendmng/zz/userIndex?apptype=2&userid=' +
          userInfo.userid +
          '&token=' +
          userInfo.token +
          '&os=' +
          (defaultTargetPlatform == TargetPlatform.android ? 'android' : 'ios'),
      initialHeaders: {},
      initialOptions: {'safeBrowsingEnabled': false},
      onWebViewCreated: (InAppWebViewController controller) {},
      onLoadStart: (InAppWebViewController controller, String url) {
        controller.addJavaScriptHandler('message', (params) {
          print(params.toString());
        });
      },
      onProgressChanged: (InAppWebViewController controller, int progress) {
        print('----------------' + progress.toString());
      },
      onConsoleMessage:
          (InAppWebViewController controller, ConsoleMessage consoleMessage) {
        print(consoleMessage.message);
      },
    );

see th picture
企业微信截图_15525518426364

@pichillilorenzo
Copy link
Owner

Yeah I know! I fixed this problem in the last commit 👍 .. I need to update the documentation because there is a little change on how it works! Later I'm going to publish the new version

@pichillilorenzo
Copy link
Owner

pichillilorenzo commented Mar 15, 2019

Feature added now on the new version 1.1.0.
Inside an html file, you may want to 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);
  });
""");

@yhdjy025
Copy link
Author

I have tested in some diff version SDK android emulator,I find some devices do not have callHandler function in window.flutter_inappbrowser object,.so I guess there has compatible problem on it.

@pichillilorenzo
Copy link
Owner

Which version?? Thanks

@yhdjy025
Copy link
Author

yhdjy025 commented Mar 18, 2019

I use the Genymotion to create a Nexus 5 - API23 of Android emulator.
look at the picture

企业微信截图_15528980511553

企业微信截图_15528981233731

And it continuous print W/art ' ( 1866): Attempt to remove non-JNI local reference, dumping thread='

@yhdjy025
Copy link
Author

I find the _callHandler function is exist,
Means the object add by addJavascriptInterface function is success, and add by evaluateJavascript is failed

@pichillilorenzo
Copy link
Owner

Ok! I will try that!

@yhdjy025
Copy link
Author

I've found the reason. look at the screenshot
企业微信截图_15529656761553

because the ...args is support in ES6, some android simulator's browser core do not support it.

This was referenced Jul 6, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants