dotnet / maui

.NET MAUI is the .NET Multi-platform App UI, a framework for building native device applications spanning mobile, tablet, and desktop.
https://dot.net/maui
MIT License
22.13k stars 1.74k forks source link

IBrowser.OpenAsync(<url>, BrowserLaunchMode.External) fails on Android #20182

Closed LasseWolter closed 4 months ago

LasseWolter commented 9 months ago

Description

Android only (works on iOS)

Steps to Reproduce

  1. Create a new MAUI project from the basic template
  2. Create a view model MainPageViewModel with the following code
    
    using System.Diagnostics;
    using System.Windows.Input;

namespace OpenBrowserAsync;

public class MainPageViewModel { public ICommand OpenBrowserCommand { get; }

public MainPageViewModel()
{
    OpenBrowserCommand = new Command(async () => await OpenBrowserAsync());
}

private async Task OpenBrowserAsync()
{
    try
    {
        await Browser.OpenAsync("https://google.com", BrowserLaunchMode.External);
    }
    catch (Exception e)
    {
        Debug.WriteLine(e);
    }
}

}

3. In the `MainPage.xaml` bind your view model to your view, e.g. using: 
```xml
<ContentPage
...  
xmlns:local="clr-namespace:OpenBrowserAsync"
...

  <ContentPage.BindingContext>
    <local:MainPageViewModel/>
  </ContentPage.BindingContext>
...
  1. On the button, remove the Clicked property and add a Command instead. Map the command to the command in your view model. Your button element should look like this:

    <Button
        Text="Click me"
        SemanticProperties.Hint="Counts the number of times you click"
        Command="{ Binding OpenBrowserCommand}"
        HorizontalOptions="Fill"/>
  2. Run on Android and click the button.

  3. App crashes.

  4. Run on iOS, click the button and the external browser opens successfully.

Link to public reproduction project repository

https://github.com/LasseWolter/NET_MAUI_IBrowser_Open_Async_Bug_Android/tree/main

Version with bug

8.0.3

Is this a regression from previous behavior?

Not sure, did not test other versions

Last version that worked well

Unknown/Other

Affected platforms

Android

Affected platform versions

Android 13 (API Level 33)

Did you find any workaround?

BrowserLaunchMode.SystemPreferred can be used in the meantime.

Relevant log output

Microsoft.Maui.ApplicationModel.FeatureNotSupportedException: Specified method is not supported.
   at Microsoft.Maui.ApplicationModel.BrowserImplementation.LaunchExternalBrowser(BrowserLaunchOptions options, Uri nativeUri) in D:\a\_work\1\s\src\Essentials\src\Browser\Browser.android.cs:line 90
   at Microsoft.Maui.ApplicationModel.BrowserImplementation.OpenAsync(Uri uri, BrowserLaunchOptions options) in D:\a\_work\1\s\src\Essentials\src\Browser\Browser.android.cs:line 23
   at Microsoft.Maui.ApplicationModel.BrowserExtensions.OpenAsync(IBrowser browser, String uri, BrowserLaunchMode launchMode) in D:\a\_work\1\s\src\Essentials\src\Browser\Browser.shared.cs:line 106
   at Microsoft.Maui.ApplicationModel.Browser.OpenAsync(String uri, BrowserLaunchMode launchMode) in D:\a\_work\1\s\src\Essentials\src\Browser\Browser.shared.cs:line 39
   at OpenBrowserAsync.MainPageViewModel.OpenBrowserAsync() in /Users/lassewolter/Git/testHandler1/OpenBrowserAsync/OpenBrowserAsync/MainPageViewModel.cs:line 19
jkommeren commented 8 months ago

Having the same issue. Pretty sure it was there in .net 7 SR3 and SR6 too.

mpcreza commented 7 months ago

same issue on 8.0.7 version

XamlTest commented 7 months ago

Verified this on VS 17.10.0 Preview 1.0(8.0.7). Repro on Android 14.0-API34, not repro on Windows, iOS 17.2 and MacCatalyst with below Project: OpenBrowserAsync.zip

Xideta commented 7 months ago

Got your sample to work.

The browser class checks if something is installed that can handle http / https links. Android, since android 11, requires you declare what schemes you check, before trying to access them. Adding the following between the \<manifest> tags of AndroidManifest.xml should do the trick (It seems to be documented too)

        <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>
LasseWolter commented 7 months ago

@Xideta, well spotted, thanks! I'll give that a shot once I get a chance and post feedback here if it fixes it my case.

smathersgit commented 6 months ago

I encountered the same issue:-

Microsoft.Maui.ApplicationModel.FeatureNotSupportedException: 'Specified method is not supported.'

and confirmed that the below AndroidManifest.xml update fixed it:

<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>

I'm using 8.0.14.

LasseWolter commented 4 months ago

Verified that amending the Android.Manifest as suggested above fixes the issue. As mentioned, this is also documented by Microsoft. Thanks a lot for the help.

Closing the issue.