Del via


WebView Mixed Content on Android

This Android platform-specific controls whether a WebView can display mixed content in applications that target API 21 or greater. Mixed content is content that's initially loaded over an HTTPS connection, but which loads resources (such as images, audio, video, stylesheets, scripts) over an HTTP connection. It's consumed in XAML by setting the WebView.MixedContentMode attached property to a value of the MixedContentHandling enumeration:

<ContentPage ...
             xmlns:android="clr-namespace:Xamarin.Forms.PlatformConfiguration.AndroidSpecific;assembly=Xamarin.Forms.Core">
    <WebView ... android:WebView.MixedContentMode="AlwaysAllow" />
</ContentPage>

Alternatively, it can be consumed from C# using the fluent API:

using Xamarin.Forms.PlatformConfiguration;
using Xamarin.Forms.PlatformConfiguration.AndroidSpecific;
...

webView.On<Android>().SetMixedContentMode(MixedContentHandling.AlwaysAllow);

The WebView.On<Android> method specifies that this platform-specific will only run on Android. The WebView.SetMixedContentMode method, in the Xamarin.Forms.PlatformConfiguration.AndroidSpecific namespace, is used to control whether mixed content can be displayed, with the MixedContentHandling enumeration providing three possible values:

  • AlwaysAllow – indicates that the WebView will allow an HTTPS origin to load content from an HTTP origin.
  • NeverAllow – indicates that the WebView will not allow an HTTPS origin to load content from an HTTP origin.
  • CompatibilityMode – indicates that the WebView will attempt to be compatible with the approach of the latest device web browser. Some HTTP content may be allowed to be loaded by an HTTPS origin and other types of content will be blocked. The types of content that are blocked or allowed may change with each operating system release.

The result is that a specified MixedContentHandling value is applied to the WebView, which controls whether mixed content can be displayed:

WebView mixed content handling platform-specific