Skip to content

Commit

Permalink
Specify WKWebViewConfiguration object on MauiMKWebView constructor (#988
Browse files Browse the repository at this point in the history
)

* MauiWKWebView configuration on iOS/Mac Catalyst.

* Edit.

* Edits.

* Move section.
  • Loading branch information
davidbritch authored Oct 12, 2022
1 parent 761fced commit 34e4016
Showing 1 changed file with 31 additions and 1 deletion.
32 changes: 31 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: 03/08/2022
ms.date: 10/11/2022
---

# WebView
Expand Down Expand Up @@ -265,6 +265,36 @@ function factorial(num) {
</html>
```

::: moniker range=">=net-maui-7.0"

## Configure the native WebView on iOS and Mac Catalyst

The native `WebView` control is a `MauiWKWebView` on iOS and Mac Catalyst, which derives from `WKWebView`. One of the `MauiWKWebView` constructor overloads enables a `WKWebViewConfiguration` object to be specified, which provides information about how to configure the `WKWebView` object. Typical configurations include setting the user agent, specifying cookies to make available to your web content, and injecting custom scripts into your web content.

You can create a `WKWebViewConfiguration` object in your app, and then configure its properties as required. Alternatively, you can call the static `MauiWKWebView.CreateConfiguration` method to retrieve .NET MAUI's `WKWebViewConfiguration` object and then modify it. The `WKWebViewConfiguration` object can then be passed to the `MauiWKWebView` constructor overload by modifying the factory method that `WebViewHandler` uses to create its native control on each platform:

```csharp
#if IOS || MACCATALYST
using WebKit;
using CoreGraphics;
using Microsoft.Maui.Platform;
using Microsoft.Maui.Handlers;
#endif
...

#if IOS || MACCATALYST
WKWebViewConfiguration config = MauiWKWebView.CreateConfiguration();
config.ApplicationNameForUserAgent = "MyProduct/1.0.0";
WebViewHandler.PlatformViewFactory =
handler => new MauiWKWebView(CGRect.Empty, (WebViewHandler)handler, config);
#endif
```

> [!NOTE]
> You should configure `MauiWKWebView` with a `WKWebViewConfiguration` object before a `WebView` is displayed in your app. Suitable locations to do this are in your app's startup path, such as in *MauiProgram.cs* or *App.xaml.cs*. <!-- For more information about configuring a native .NET MAUI control, see [Customize controls](~/user-interface/handlers/customize.md). -->
::: moniker-end

## Launch the system browser

It's possible to open a URI in the system web browser with the `Launcher` class, that's provided by `Microsoft.Maui.Essentials`. This is achieved by calling it's `OpenAsync` method, passing in a `string` or `Uri` argument that represents the URI to open:
Expand Down

0 comments on commit 34e4016

Please sign in to comment.