Nativescript: Prevent WebView from loading specific URL

Created on 1 Nov 2017  路  5Comments  路  Source: NativeScript/NativeScript

Looking into the source code for both iOS and Android, it looks there is no way to prevent a WebView from loading a specific URL.

Am I missing something?

If not, we need this functionality and most probably we'll implement it in a fork of {N} and create a PR if this is something anybody else would need.

Most helpful comment

Hi @surdu,
In your case, you could use loadStartedEvent to validate the url and if needed to stop page loading while using stopLoading() method. For example:

    webview.on(WebView.loadStartedEvent, (args: LoadEventData)=>{
        if(args.url === "url"){
            (<WebView>args.object).stopLoading();
        }
    })

Let me know if this helps in your case.

All 5 comments

Hi @surdu,
In your case, you could use loadStartedEvent to validate the url and if needed to stop page loading while using stopLoading() method. For example:

    webview.on(WebView.loadStartedEvent, (args: LoadEventData)=>{
        if(args.url === "url"){
            (<WebView>args.object).stopLoading();
        }
    })

Let me know if this helps in your case.

@tsonevn Ha! Yes, it does the trick! I didn't think of that :)

Thanks!

The stopLoading seems sometimes not working for Android platform, sometimes it still loading and redirect to the specific url

Hi @LSmint,
Can you provide sample project, which could be used for debugging?

@tsonevn I can confirm the problem, the provided solution is not working (also on iOS)

Was this page helpful?
0 / 5 - 0 ratings

Related issues

rogangriffin picture rogangriffin  路  3Comments

guillaume-roy picture guillaume-roy  路  3Comments

minjunlan picture minjunlan  路  3Comments

Pourya8366 picture Pourya8366  路  3Comments

yclau picture yclau  路  3Comments