How do I get my .net maui app to appear in the device Share dialog?

643 Views Asked by At

Often, apps have a "Share" button. When using other apps on my device which have the "Share" button (e.g. web browser, news apps, etc), when you tap this button it shows you a list of apps to share the URL with.

So, for example, I can be in Chrome, navigate to a page, tap Share, and from this I can select my email app to share the URL.

Any advice on how that can be implemented in Maui? I.e. I want my app to appear in the Share dialog.

Thanks

P.S. apologies for the poor wording before ;)

2

There are 2 best solutions below

7
On

Here's what works for me in Android platform. I can launch my app one of two ways:


Navigate to https://www.ivsoftware.net/interceptor-test.html and click the button:

deep-linking


From this or any other web page, click the Google Chrome Share Icon instead:

share icon


Here's the Android Intent Filter code.

using Android.App;
using Android.Content;
using Android.OS;
using url_interceptor;

namespace url_interceptor.Platforms.Android
{
    [Activity(Label = "UrlInterceptorActivity", Exported =true)]
    [IntentFilter(
        new[] { Intent.ActionView },
        Categories = new[]
        { 
            Intent.CategoryDefault, 
            Intent.CategoryBrowsable 
        },
        DataSchemes = new[] 
        {
            "net.ivsoftware.demo" 
        })]

    [IntentFilter(
        new[]
        { Intent.ActionSend },
        Categories = new[]
        {
            Intent.CategoryDefault,
        },
        DataMimeType = "text/plain"
    )]
    public class UrlInterceptorActivity : Activity
    {
        protected override void OnCreate(Bundle savedInstanceState)
        {
            base.OnCreate(savedInstanceState);

            Intent intent = Intent;
            var uri = intent.Data;
            StartMainActivity();
            Task
                .Delay(TimeSpan.FromSeconds(0.5))
                .GetAwaiter()
                .OnCompleted(() =>
                {
                    switch (this.Intent.Action)
                    {
                        case Intent.ActionView:
                            if (intent.Action == Intent.ActionView)
                            {
                                var uri = intent.Data;
                                App.Current.MainPage.DisplayAlert("Interceptor", "Deep Link Button", "OK");
                            }
                            break;
                        case Intent.ActionSend:
                            var link = intent.GetStringExtra(Intent.ExtraText);
                            App.Current.MainPage.DisplayAlert("Interceptor", $"Shared Link: '{link}'", "OK");
                            break;
                    }
                });
            Finish(); 
        }

        private void StartMainActivity()
        {
            Intent mainActivityIntent = new Intent(this, typeof(MainActivity));
            StartActivity(mainActivityIntent);
        }
    }
}

iOS has a similar mechanism for intercepting URLs but I haven't tested this particular action.

3
On

MAUI provides IShare interface for such purpose. Just use RequestAsync method to show list of apps to share.

await Share.Default.RequestAsync(new ShareTextRequest
{
    Uri = "https://test.com",
    Title = "Share Web Link"
});

More info can be found here: https://learn.microsoft.com/en-us/dotnet/maui/platform-integration/data/share?tabs=android