Browser

Browse sample. Browse the sample

This article describes how you can use the .NET Multi-platform App UI (.NET MAUI) IBrowser interface. This interface enables an application to open a web link in the system-preferred browser or the external browser.

The default implementation of the IBrowser interface is available through the Browser.Default property. Both the IBrowser interface and Browser class are contained in the Microsoft.Maui.ApplicationModel namespace.

Get started

To access the browser functionality, the following platform-specific setup is required.

If your project's Target Android version is set to Android 11 (R API 30) or higher, you must update your Android Manifest with queries that use Android's package visibility requirements.

In the Platforms/Android/AndroidManifest.xml file, add the following queries/intent nodes in the manifest node:

<queries>
  <intent>
    <action android:name="android.intent.action.VIEW" />
    <data android:scheme="http"/>
  </intent>
  <intent>
    <action android:name="android.intent.action.VIEW" />
    <data android:scheme="https"/>
  </intent>
</queries>

Open the browser

The browser is opened by calling the IBrowser.OpenAsync method with the Uri and the type of BrowserLaunchMode. The following code example demonstrates opening the browser:

private async void BrowserOpen_Clicked(object sender, EventArgs e)
{
    try
    {
        Uri uri = new Uri("https://www.microsoft.com");
        await Browser.Default.OpenAsync(uri, BrowserLaunchMode.SystemPreferred);
    }
    catch (Exception ex)
    {
        // An unexpected error occurred. No browser may be installed on the device.
    }
}

This method returns after the browser is launched, not after it's closed by the user. Browser.OpenAsync returns a bool value to indicate if the browser was successfully launched.

Customization

If you're using the system-preferred browser, there are several customization options available for iOS and Android. These options include a TitleMode (Android only) and preferred color for the Toolbar (iOS and Android) and Controls (iOS only) that appear.

Specify these options using BrowserLaunchOptions when you call OpenAsync.

private async void BrowserCustomOpen_Clicked(object sender, EventArgs e)
{
    try
    {
        Uri uri = new Uri("https://www.microsoft.com");
        BrowserLaunchOptions options = new BrowserLaunchOptions()
        {
            LaunchMode = BrowserLaunchMode.SystemPreferred,
            TitleMode = BrowserTitleMode.Show,
            PreferredToolbarColor = Colors.Violet,
            PreferredControlColor = Colors.SandyBrown
        };

        await Browser.Default.OpenAsync(uri, options);
    }
    catch (Exception ex)
    {
        // An unexpected error occurred. No browser may be installed on the device.
    }
}

Platform differences

This section describes the platform-specific differences with the browser API.

The BrowserLaunchOptions.LaunchMode determines how the browser is launched:

  • SystemPreferred

    Custom Tabs are used to load the URI and keep navigation awareness.

  • External

    An Intent is used to request the URI be opened through the system's normal browser.