Del via


Xamarin.Essentials: Browser

The Browser class enables an application to open a web link in the optimized system preferred browser or the external browser.

Get started

To start using this API, read the getting started guide for Xamarin.Essentials to ensure the library is properly installed and set up in your projects.

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) you must update your Android Manifest with queries that are used with the new package visibility requirements.

Open the AndroidManifest.xml file under the Properties folder and add the following inside of 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>

Using Browser

Add a reference to Xamarin.Essentials in your class:

using Xamarin.Essentials;

The Browser functionality works by calling the OpenAsync method with the Uri and BrowserLaunchMode.


public class BrowserTest
{
    public async Task OpenBrowser(Uri uri)
    {
        try
        {
            await Browser.OpenAsync(uri, BrowserLaunchMode.SystemPreferred);
        }
        catch(Exception ex)
        {
            // An unexpected error occured. No browser may be installed on the device.
        }
    }
}

This method returns after the browser was launched and not necessarily closed by the user.

Customization

When using the system preferred browser there are several customization options available for iOS and Android. This includes a TitleMode (Android only), and preferred color options for the Toolbar (iOS and Android) and Controls (iOS only) that appear.

These options are specified using BrowserLaunchOptions when calling OpenAsync.

await Browser.OpenAsync(uri, new BrowserLaunchOptions
                {
                    LaunchMode = BrowserLaunchMode.SystemPreferred,
                    TitleMode = BrowserTitleMode.Show,
                    PreferredToolbarColor = Color.AliceBlue,
                    PreferredControlColor = Color.Violet
                });

Browser Options

Platform Implementation Specifics

The Launch Mode determines how the browser is launched:

System Preferred

Custom Tabs will attempted to be used to load the Uri and keep navigation awareness.

External

An Intent will be used to request the Uri be opened through the systems normal browser.

API

Find more Xamarin videos on Channel 9 and YouTube.