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); } }
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.
Also if the android app is not installed on the device, need to open the play store to download the app.
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.
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
You could change your the code in Android like following.(Note:It's the package name of the google play store (or other app store) on your device )
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 .
https://stackoverflow.com/questions/43944283/launch-another-ios-app-from-xamarin-forms-app
Answers
You could change your the code in Android like following.(Note:It's the package name of the google play store (or other app store) on your device )
For iOS you could refer Launch Another IOS App from Xamarin Forms App .
https://stackoverflow.com/questions/43944283/launch-another-ios-app-from-xamarin-forms-app
@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?