NativeScript WebView open url in default browser

半腔热情 提交于 2021-01-28 08:02:52

问题


I am trying to build app with WebView and click/tap events on URL-s inside WebView. Solution below opens external browser and URL but it loads same url content in webview as well. Is there a way to prevent loading new url inside webview?

Here is my code sample.

function onWebViewLoaded(webargs) {
const page = webargs.object.page;
const vm = page.bindingContext;
const webview = webargs.object;


webview.on(webViewModule.WebView.loadFinishedEvent, (args) => {
   let message = "Loading in progress....";
   if (!args.error) {
    message = `WebView loading finished with url: ${args.url}`;
} else {
    message = `Error received ${args.url} : ${args.error}`;
}


   if (args.url.indexOf('http://') === 0) {
    // Stop the loading url first... but how..

    // Open URL in external default browser
    utilityModule.openUrl(args.url);
}
});
}

I have tried with setting a flag isUserInteractionEnabled="false" added to my xml view but then all interactions are disabled. Does someone knows how to do this?


回答1:


Try with loadStartedEvent

    webview.on(webViewModule.WebView.loadStartedEvent, (args) => {
        if(!args.url.includes("whatever.net")){
            webview.stopLoading();
            utilsModule.openUrl(args.url);
        }
    });

This way we catch the loadStartedEvent instead of loadFinishedEvent (where the url is already loaded), we check the url if we want to filter depending on it, and here we can webview.stopLoading() for stop the page load.



来源:https://stackoverflow.com/questions/51355666/nativescript-webview-open-url-in-default-browser

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!