[Vuejs]-Set listener on WebView | nativescript + vue

0👍

Ok I got the answer:

there is some issues here:


#1. implanting nativescript-webview-interface.js:

I need to copy this file:

cp node_modules/nativescript-webview-interface/www/nativescript-webview-interface.js app/assets/www/lib/


#2. permissions!

but #1 causes another problem: net::ERR_ACCESS_DENIED

for solving this one I add this:

setupWebViewInterface(page){
    let webView = page.getViewById('webView');
    webView.android.getSettings().setAllowFileAccess(true);
}

and now I have access to the folder so I moved this.html variable content to a new file : /assets/www/index.html


#3. setting src

since just now we have access to the folder, we need to (re)set the src of web-view so I add this one to the setupWebViewInterface Function:

this.oWebViewInterface = new webViewInterfaceModule.WebViewInterface( webView, '~/assets/www/index.html' );

note: I removed src from <webView /> since it causes problem! (if old and new src would be the same it will not reload!)


#4.defining Variables:

in html file I need to add this line:

var oWebViewInterface = window.nsWebViewInterface;

#5. defining listener:

I add this one: oWebViewInterface.emit('anyEvent', event);
inside this function:

function onPlayerStateChange(event) {
    console.log("state changed!");
    oWebViewInterface.emit('anyEvent', event);
}

Now it works 🙂

Leave a comment