6

I'm looking for the event of a Android WebView that is equivalent to the "Navigating" event of a WPF WebBrowser, or the "ShouldStartLoad" of a IOS UiWebView.

I found "shouldOverrideUrlLoading", but I don't know how to use it because the constructor needs an URL...

I would like to intercept the Url before navigating, and cancel the navigation for some URLs...

I'm using Xamarin in Visual Studio.

WPF equivalent :

webBrowser.Navigating += WebBrowserNavigating;

private void WebBrowserNavigating(object sender, NavigatingEventArgs e)
    {
        if (//condition)
        e.Cancel = true;
        //Do something else
    }
Gab
  • 1,861
  • 5
  • 26
  • 39

1 Answers1

8

Yes, you should use the shouldOverrideUrlLoading method. This method is called automatically, so you don't have to worry about the parameters. In order to use this method, you have to create first a WebViewClient like this:

private class HelloWebViewClient extends WebViewClient {

     @Override
   public boolean shouldOverrideUrlLoading(WebView view, String url) {
         ...
      }
}

And then, you set it as your WebViewClient:

webview.setWebViewClient(new HelloWebViewClient());

When the shouldOverrideUrlLoading method is called you'll receive by parameter the WebView and the URL that is going to be loaded.

belen
  • 296
  • 1
  • 5
  • Thanks for the answer! I have an other question : is there an equivalent to "UIWebView.EvaluateJavascript" or "WebBrowser.InvokeScript" ? – Gab May 09 '14 at 09:25
  • There's this method: webview.getSettings().setJavaScriptEnabled(true); but I don't think it's the same thing – belen May 10 '14 at 09:09
  • @Gab i am also struggle with that place xamarin forms webview navigated method get response token get successfully in androdi but in iOS i am getting passed URL value only after login clicked . what problem there. are u have solution let u tell me – Bhuvaneshwaran Vellingiri Mar 15 '18 at 11:39