Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Disable support for multiple windows in a WebView on Android #2742

Merged
merged 3 commits into from
Jan 17, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 18 additions & 1 deletion docs/user-interface/controls/webview.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
---
title: "WebView"
description: "This article explains how to use the .NET MAUI WebView to display remote web pages, local HTML files, and HTML strings."
ms.date: 08/30/2024
ms.date: 01/17/2025
zone_pivot_groups: devices-platforms
---

Expand Down Expand Up @@ -231,6 +231,23 @@ When page navigation occurs in a <xref:Microsoft.Maui.Controls.WebView>, either

:::zone pivot="devices-android"

## Navigate to content that opens a new Window on Android

On Android, navigation won't occur in a <xref:Microsoft.Maui.Controls.WebView> when a hyperlink that specifies `target="_blank"` (to open the content in a new window) is pressed. This is because opening a hyperlink in a new window requires <xref:Android.Webkit.WebChromeClient.OnCreateWindow%2A> to be implemented, which .NET MAUI doesn't. Therefore, for this scenario you should decide whether to implement <xref:Android.Webkit.WebChromeClient.OnCreateWindow%2A> yourself, open the URL in the system browser, or do something else.

Alternatively, to force all hyperlinks to open in the same <xref:Microsoft.Maui.Controls.WebView>, modify the `WebViewHandler` in your app so that the native <xref:Android.Webkit.WebView> disables support for multiple windows:

```csharp
#if ANDROID
Microsoft.Maui.Handlers.WebViewHandler.Mapper.AppendToMapping("SupportMultipleWindows", (handler, view) =>
{
handler.PlatformView.Settings.SetSupportMultipleWindows(false);
});
#endif
```

This code customizes the property mapper for the `WebViewHandler` on Android by calling the <xref:Android.Webkit.WebSettings.SetSupportMultipleWindows%2A> method with a `false` argument, and should be executed before a user can navigate to a hyperlink that specifies `target="_blank"`. For more information about handlers, see [Handlers](~/user-interface/handlers/index.md).

## Handle permissions on Android

When browsing to a page that requests access to the device's recording hardware, such as the camera or microphone, permission must be granted by the <xref:Microsoft.Maui.Controls.WebView> control. The `WebView` control uses the <xref:Android.Webkit.WebChromeClient?displayProperty=fullName> type on Android to react to permission requests. However, the `WebChromeClient` implementation provided by .NET MAUI ignores permission requests. You must create a new type that inherits from `MauiWebChromeClient` and approves the permission requests.
Expand Down
Loading