Forum Xamarin.Forms
We are excited to announce that the Xamarin Forums are moving to the new Microsoft Q&A experience. Q&A is the home for technical questions and answers at across all products at Microsoft now including Xamarin!

We encourage you to head over to Microsoft Q&A for .NET for posting new questions and get involved today.

Xamarin Forms: How to open an App from the Another App?

SreeeeSreeee INMember ✭✭✭✭✭
edited August 26 in Xamarin.Forms

I have 2 applications ( A and B ) developed using xamarin forms. I need to open the A app from the B app.

I have tried like below as per this thread:

In the view

var appname = @"otherappId";
var result = await DependencyService.Get<IAppHandler>().LaunchApp(appname);

Interface

public interface IAppHandler
{
    Task<bool> LaunchApp(string uri);
}

Android

[Activity(Label = "OpenAppAndroid")]
public class OpenAppAndroid : Activity, IAppHandler
{
    public Task<bool> LaunchApp(string uri)
    {
        bool result = false;

        try
        {
            var aUri = Android.Net.Uri.Parse(uri.ToString());
            var intent = new Intent(Intent.ActionView, aUri);
            Xamarin.Forms.Forms.Context.StartActivity(intent);
            result = true;
        }
        catch (ActivityNotFoundException)
        {
            result = false;
        }

        return Task.FromResult(result);
    }
}

IOS

public class OpenAppiOS : IAppHandler
{
    public Task<bool> LaunchApp(string uri)
    {
        try
        {
            var canOpen = UIApplication.SharedApplication.CanOpenUrl(new NSUrl(uri));

            if (!canOpen)
                return Task.FromResult(false);

            return Task.FromResult(UIApplication.SharedApplication.OpenUrl(new NSUrl(uri)));

        }
        catch (Exception ex)
        {
            return Task.FromResult(false);
        }
    }
  1. For android, I am getting System.NullReferenceException: 'Object reference not set to an instance of an object.' when running the project. I don't know what is app name in the code? I have tried with the package name.

  2. Also if the android app is not installed on the device, need to open the play store to download the app.

  3. I didn't test the ios part because Mac is not available. Is the above code work for ios? Also if the app is not installed on the iPhone, need to open the Appstore to download the app.

  4. Also, I like to implement the same for UWP.

Reference:
https://forums.xamarin.com/discussion/92666/detect-and-open-another-app-on-device
https://stackoverflow.com/questions/52062594/is-it-possible-to-open-another-app-in-my-app-with-xamarin-form

Best Answer

Answers

  • SreeeeSreeee INMember ✭✭✭✭✭

    @LeonLu The else part is not working for me. If the app is not installed in the device I need to load the play store app page.
    I have tried like below, is there any issue if I use like this?

    if (IsAppInstalled(packageName))
    {
        Intent intent = pm.GetLaunchIntentForPackage(packageName);
        if (intent != null)
        {
            intent.SetFlags(ActivityFlags.NewTask);
            Android.App.Application.Context.StartActivity(intent);
        }
    }
    else
    {
        Device.OpenUri(new Uri("play store app link"));
    }
    
Sign In or Register to comment.