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.
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<bool> OpenBrowser(Uri uri)
{
return await Browser.OpenAsync(uri, BrowserLaunchMode.SystemPreferred);
}
}
This method returns after the browser was launched and not necessarily closed by the user. The bool
result indicates whether the launching was successful or not.
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
});
Platform Implementation Specifics
The Launch Mode determines how the browser is launched:
System Preferred
Chrome Custom Tabs will attempted to be used 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
Related Video
Feedback
Loading feedback...