7

I have 2 applications ( A and B ) developed using Xamarin.Forms. I need to open app A from app B.

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, it needs 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 Is it possible to open another app in my app with xamarin.form?

Vyrd
  • 172
  • 2
  • 12
Sreejith Sree
  • 3,055
  • 4
  • 36
  • 105

2 Answers2

7

Xamarin.Forms.Forms.Context is out of date since XF 2.5 . So you could use Android.App.Application.Context instead of it or use the plugin Plugin.CurrentActivity

So you could modify the code in Android like following

using System.Threading.Tasks;
using Android.App;
using Android.Content;
using Android.Content.PM;
using Android.OS;
using Android.Runtime;
using Android.Views;
using Android.Widget;
using App14;
using App14.Droid;
using Xamarin.Forms;

[assembly: Dependency(typeof(OpenAppAndroid))]
namespace App14.Droid
{
    public class OpenAppAndroid : IAppHandler
    {
        public Task<bool> LaunchApp(string packageName)
        {


            bool result = false;

            try
            {

                PackageManager pm = Android.App.Application.Context.PackageManager;

                if (IsAppInstalled(packageName))
                {
                    
                    Intent intent = pm.GetLaunchIntentForPackage(packageName);
                    if (intent != null)
                    {
                      
                        intent.SetFlags(ActivityFlags.NewTask);
                        Android.App.Application.Context.StartActivity(intent);
                    }
                }

                else
                {
                  
                    Intent intent = pm.GetLaunchIntentForPackage("the package name of play store on your device");
                    if (intent != null)
                    {

                        intent.SetFlags(ActivityFlags.NewTask);
                        Android.App.Application.Context.StartActivity(intent);
                    }
                }
            }
            catch (ActivityNotFoundException)
            {
                result = false;
            }

            return Task.FromResult(result);
        }


        private bool IsAppInstalled(string packageName)
        {
            PackageManager pm = Android.App.Application.Context.PackageManager;
            bool installed = false;
            try
            {
                pm.GetPackageInfo(packageName, PackageInfoFlags.Activities);
                installed = true;
            }
            catch (PackageManager.NameNotFoundException e)
            {
                installed = false;
            }

            return installed;
        }

    }
}

For iOS you could refer Launch Another IOS App from Xamarin Forms App .

Lucas Zhang
  • 18,630
  • 3
  • 12
  • 22
  • It's the package name of the google play store (or other app store) on your device . You need to check it . – Lucas Zhang Aug 26 '20 at 12:45
  • `Device.OpenUri(new Uri("xxx"))` – Lucas Zhang Aug 26 '20 at 12:55
  • I am facing an issue for the same feature in ios app. could you please have a look? https://stackoverflow.com/questions/63751993/xamarin-forms-issue-with-launching-ios-app-from-xamarin-forms-app-invalid-inpu – Sreejith Sree Sep 06 '20 at 09:50
6

Have you tried using Xamarin Essentials : Xamarin.Essentials: Launcher

   public class LauncherTest
{
    public async Task OpenRideShareAsync()
    {
        var supportsUri = await Launcher.CanOpenAsync("lyft://");
        if (supportsUri)
            await Launcher.OpenAsync("lyft://ridetype?id=lyft_line");
    }
}

don't forget to use the correct package name when opening specific apps

Ibrennan208
  • 1,345
  • 3
  • 14
  • 31
LeRoy
  • 4,189
  • 2
  • 35
  • 46
  • SO fyi, this answer is for URI endpoints, not the same as launching another apk – DukeDidntNukeEm Aug 25 '21 at 22:23
  • @DukeDidntNukeEm this is for APK's if you wanted to open a browser you would use the following code await Browser.OpenAsync(uri, BrowserLaunchMode.SystemPreferred); – LeRoy Aug 31 '21 at 10:03
  • Thanks! maybe I'm confused, I thought a URI invokes an apk but to directly invoke an apk in code you have to do it differently than the answer here - does that make sense? like say, you want to open google maps but not starting with a clickable location or something, just loading the google maps apk :) – DukeDidntNukeEm Aug 31 '21 at 18:34