Understanding the new features of WebView in Universal Windows App.

By | October 11, 2015

WebView class in Universal Windows Platform has been updated with the new features. The WebView control helps the developers to display the content of an website inside the App. Some of the new events that are introduced in the web view class are highly useful for the developers to provide a better user experience to the users. Lets see the new events in details.

WebView

NewWindowRequested Event.

Some of the websites request the some pages to be opened in a new window. When such a site is getting displayed in a WebView then the app opens that site in the default browser when requested for a new window. This is actually a bad user experience, which can be solved with the help of this new event called NewWindowRequested. this event gets triggered when ever a page in webview request for the new window. Developer can get the requested new URL and then assign it to a another WebView or to the same WebView and make the page displayed in the same App instead of opening it in a default browser. Always set the handle = true at the end so that the default action will not be called.

 private void WebView_NewWindowRequested(WebView sender, WebViewNewWindowRequestedEventArgs args)
 {
 var newWebView = new WebView();
 newWebView.Navigate(args.Uri);
 myGrid.Children.Add(newWebView);
 args.Handled = true;
 }

UnsupportedUriSchemeIdentified Event.

Normally WebView takes the an URI and navigate to that, but when a URL specified to a WebView is unsupported by the WebView then the navigation is blocked. WebView supports the following navigation to URIs: http, https, ms-appx-web, and ms-local-stream.
If any other URl is provided like mailto which dosent supported by the WebView then the launcher is invoked to identify the default provider for that URI. This UnsupportedUriSchemeIdentified Event helps the developer to handle the situation instead of invoking the launcher. Always set the handle = true at the end so that the default action will not be called.

<pre>private void WebView_UnsupportedUriSchemeIdentified(WebView sender, WebViewUnsupportedUriSchemeIdentifiedEventArgs args)
{
if (args.Uri.Scheme == "mailto")
{
args.Handled = true;
}
}

PermissionRequested Event

When we display an website in our WebView, if that website request the user to use the geolocation by default the WebView deny the permission. So to avoid this situation we can use the PermissionRequested Event and let the developer or user to decide whether the website can access the geolocation or not. Developer can Allow, Deny or Defer the request in case he wants to get confirm from the user. The types of permissions that can be requested by the websites are GeolocationUnlimitedIndexedDBQuotaMedia.


private void WebView_PermissionRequested(WebView sender, WebViewPermissionRequestedEventArgs args)
{
if(args.PermissionRequest.PermissionType == WebViewPermissionType.Geolocation)
{
args.PermissionRequest.Allow();
}
}

WebView

Hope this might have helped.

3 thoughts on “Understanding the new features of WebView in Universal Windows App.

  1. Pingback: Dew Drop – October 12, 2015 (#2109) | Morning Dew

  2. Pingback: Visual Studio – Developer Top Ten for Oct 15th, 2015 - Dmitry Lyalin

  3. Pingback: Szumma #012 – 2015 42. hét | d/fuel

Comments are closed.