Google Xamarin.Forms: how to prevent to open specific Url links from WebView (C# - Xaml) | SubramanyamRaju Xamarin & Windows App Dev Tutorials

Thursday, 5 March 2020

Xamarin.Forms: how to prevent to open specific Url links from WebView (C# - Xaml)

Introduction:
We know that WebView is a view for displaying web and HTML content in your app. Due to security, sometimes we may need to restrict to open specific urls in our app. 

Description:
In Xamarin.Forms, WebView raises the following events to help you respond to changes in state:
  • Navigating – event raised when the WebView begins loading a new page.
  • Navigated – event raised when the page is loaded and navigation has stopped.
  • ReloadRequested – event raised when a request is made to reload the current content.
So we can use Navigating event, because this event args has a Cancel property that helpful to cancel the new url/page navigation like below

In MainPage.xaml
  1. <WebView Navigating="OnNavigating" />  

In MainPage.xaml.cs

  • async void OnNavigating(object sender, WebNavigatingEventArgs args)  
  • {  
  •     // args.url will have new page url  
  •     if (args.Url == "https://google.com")  
  •     {  
  •         args.Cancel = true;  
  •         await DisplayAlert("Info""This link was restricted to open in this app.""Ok");  
  •     }  
  • }  


  • FeedBack Note: Please share your thoughts, what you think about this post, Is this post really helpful for you? I always welcome if you drop comments on this post and it would be impressive.

    Follow me always at @Subramanyam_B

    No comments:

    Post a Comment

    Search Engine Submission - AddMe