shripalsoni04 / nativescript-webview-interface

Plugin for bi-directional communication between webView and android/ios
MIT License
89 stars 35 forks source link

Emitting event to native app not working on Android #4

Closed ghost closed 8 years ago

ghost commented 8 years ago

Hi,

in the app i do;

oWebViewInterface.on('evseidToLoadingScreen', function(evseid) {
    t.log('NATIVE:'+evseid);
});

in the webview:

oWebViewInterface.emit('evseidToLoadingScreen', '#43#0815');

nothing happens.

i also do in app:

oWebViewInterface.callJSFunction('initMap', {
    lat: lat,
    lng: lng,
    markerApiUrl: markerApiUrl,
    chargePointDetailUrl: chargePointDetailUrl
}, function(result) {
    t.log(':');
    t.log(result);
    t.log(':');
});

That works partially: initMap is getting called in the webview, but the success callback is never called.

It seems, native to webview is working fine, but from webview back to native not at all.

What could that be?

edit: on ios the code works perfectly fine, android version is 6.0.1

ghost commented 8 years ago

@shripalsoni04 inside my webview there is no window.androidWebViewInterface object, maybe thats the reason?

shripalsoni04 commented 8 years ago

@einicher I tried to run your code in this demo app on android 6.0. It is working properly there. I have run the app with nativescript version 2.1.1.

Can you please try to clone that demo app, and run on android 6.0.1 with latest version of nativescript and runtime. If demo app works, just add your code in that app and check it.

If your code works proper in the demo app, then upgrade tns-core-modules package and remove/add android platform in your project. This should resolve the issue.

Hope this will help.

ghost commented 8 years ago

Adding new languages works, but the two buttons beneath do nothing. "tns-android":"version": "2.1.1" "tns-core-modules": "^2.1.0"

shripalsoni04 commented 8 years ago

Thanks for testing the demo app.

Actually I tested it yesterday on both emulator and device for android 6.0. It was working fine there. So it is strange why it is not working.

I will try to test it with android 6.0.1. Also, can you please tell me are you testing on emulator or device?

ghost commented 8 years ago

device, but will test it with emulator and over the mac too.

davistan commented 8 years ago

Happened to my side as well on Android 5.0. However, it did work (intermittently) when I added alert() code in native side.

davistan commented 8 years ago

BTW, I also noticed that alert() code in the webview does not work. And from an answer found in SO is due to this line: webView.setWebChromeClient(new WebChromeClient());

Perhaps is this related?

davistan commented 8 years ago

@shripalsoni04 Looks like there's a timing (race condition) that the .on() maybe too early. Perhaps some initialized event to hook on before the on() to be executed?

shripalsoni04 commented 8 years ago

I just pushed android code for this plugin. As I am still not able to replicate this issue at my end, you can experiment at your end by modifying the java code.

Code and Instruction to generate .jar file can be found at https://github.com/shripalsoni04/android-webview-interface.

Hope this will help you.

shripalsoni04 commented 8 years ago

Finally I was able to reproduce this issue on one android device. The issue was, sometimes the web-view is getting loaded before the plugin sets javascriptInterface to setup communication from web-view to android. And if javascriptInterface is set after web-view is loaded, it will not be accessible in web-view, which results in issue of communication from web-view to android sometime.

I have created a patch in version 1.3.1 and added src parameter in WebViewInterface constructor. To make it work on all devices, instead of setting src in xml file, you need to set it by passing it as argument in the constructor as shown below:

var webViewInterface = new webViewInterfaceModule.WebViewInterface(webView, '~/www/index.html');

I have also updated the demo app to demonstrate this change.

Check below diff of sample demo app to understand it quickly. main-page.xml https://github.com/shripalsoni04/nativescript-webview-interface-demo/commit/64725b9f8ba48470acf9bc28e5d7b364fe7d9930#diff-585f95637116edda1e0b367ddbfbc6f1L5

main-page.ts https://github.com/shripalsoni04/nativescript-webview-interface-demo/commit/64725b9f8ba48470acf9bc28e5d7b364fe7d9930#diff-422bc91ebf1044e50f02c0757b4e30cfL35

So just update the plugin to latest version and do the above mentioned changes.

Thanks a lot @einicher and @davistan for raising this issue.

ghost commented 8 years ago

Your demo app works, but my app still does not. Is it possible the reason is, because I have the WebView in A TabView?

ghost commented 8 years ago

ok, it works now, reason seemed to be the segmented bar.