Notalib / nativescript-webview-ext

Nativescript plugin with extended WebView functionality
Apache License 2.0
76 stars 37 forks source link

Emitting event to webview doesn't work #48

Closed romanran closed 5 years ago

romanran commented 5 years ago

I'm using vue, everything works fine, but the events.

<template>
<Page class="map page" actionBarHidden="true" height="100%">
    <WebViewExt @loaded="webviewLoaded" @webAlert="alert" @loadFinished="webviewFinishedLoading" src="~/assets/test/index.html" :debugMode="isDevelopment"></WebViewExt>
</Page>
</template>

<script>
import * as geolocation from 'nativescript-geolocation'

export default {
    data() {
        return {
            watcher: 0,
            isDevelopment: TNS_ENV === 'development',
            position: [0, 0]
        }
    },
    created() {},
    methods: {
        alert({message }) {
            console.log('alert', message);
        },
        webviewFinishedLoading({object}) {
            const webview = object
            webview.loadStyleSheetFile("test.css", "~/assets/test/test.1.css")
            webview.loadJavaScriptFile("app.js", "~/assets/test/js/app.3.js")
        },
        webviewLoaded({object}) {
            const webview = object
            this.watcher = geolocation.watchLocation(({latitude, longitude}) => {
                this.position = [latitude, longitude]
                webview.emitToWebView('test', 'test string from app')
                console.log([latitude, longitude]);
            }, err => {
                console.warn('error', err)
            }, {
                updateTime: 10000
            })
        }
    },
    destroy() {
        geolocation.clearWatch(this.watcher)
    }
}
</script>

Which platform(s) does your issue occur on?

Please, provide the following version numbers that your issue occurs with:

Please, tell us how to recreate the issue in as much detail as possible.

app.3.js alert('test') window.nsWebViewBridge.on('test', function(cos) { alert('test event') }) I'm getting the "test" string, console.log with positions is working, but the test event doesn't return alert message.

romanran commented 5 years ago

I'm checking the plugins code and I think it works different then I thought. Also I made sure that window.nsWebViewBridge is available, in the webview script.

m-abs commented 5 years ago

Hi @romanran

Thank you for the issue.

Two things comes to mind. First are you sure the assets are bundle with your app? This requires a small modification to webpack.config.js and is a very common cause for problems.

            new CopyWebpackPlugin([
                { from: { glob: "fonts/**" } },
                { from: { glob: "**/*.jpg" } },
                { from: { glob: "**/*.png" } },
                { from: { glob: 'assets/**'} }, // add this
            ])

Secondly could you try the alpha version of the plugin? It has been updated to nativescript-6.x, but is in alpha because our own app is still on 5.4.x.

romanran commented 5 years ago

Yes, all other features are working, I'm sure of it. I switched to another plugin already so I won't check unfortunately. Thanks for help.