James Cheevers
James Cheevers

Reputation: 35

How to open default email app inbox in Xamarin Forms

I'm working on a Xamarin Forms app and am trying to open the the default mail client directly to the Inbox.

I'm able to open and pass data through to compose a message using XF Essentials

Email.ComposeAsync(message);

But I would like the app to open the default mail app's Inbox on a button press. Is this possible in Xamarin Forms?

Upvotes: 1

Views: 1545

Answers (1)

Ax1le
Ax1le

Reputation: 6641

I think Dependency Service is what you need. Create an interface in your Forms project:

public interface IOpenManager
{
    void openMail();
}

Then implement it on each platform, for iOS:

[assembly: Dependency(typeof(OpenImplementation))]
namespace Demo.iOS
{
    public class OpenImplementation : IOpenManager
    {
        public void openMail()
        {
            NSUrl mailUrl = new NSUrl("message://");
            if (UIApplication.SharedApplication.CanOpenUrl(mailUrl))
            {
                UIApplication.SharedApplication.OpenUrl(mailUrl);
            }            
        }
    }
}

For Android:

[assembly: Dependency(typeof(OpenImplementation))]
namespace Demo.Droid
{
    public class OpenImplementation : IOpenManager
    {
        public void openMail()
        {
            Intent intent = new Intent(Intent.ActionMain);
            intent.AddCategory(Intent.CategoryAppEmail);
            Android.App.Application.Context.StartActivity(intent);
        }
    }
}

At last, call this dependcy via: DependencyService.Get<IOpenManager>().openMail();

Upvotes: 3

Related Questions