Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I natively launch an external app from within Xamarin.Forms?

As the question title suggests, I'm looking for a way to launch an external app from within a Xamarin.Forms app. For example, my app has a list of addresses, and when the user taps on one of them, the built-in map app for the current platform would open (Google Maps for Android, Apple Maps for iOS). In case it matters, I am only targeting Android and iOS.

I could use a dependency service and write the app-launching code on a per-platform basis, of course, but I'd prefer if I only had to write it once. Is there a native way to do this in Xamarin.Forms? I was unable to find anything that officially documented this on the Xamarin site or forums.

like image 967
Mage Xy Avatar asked Dec 28 '25 16:12

Mage Xy


2 Answers

Use Device.OpenUri and pass it the appropriate URI, combined with Device.OnPlatform to format the URI per platform

string url; 

Device.OnPlatform(iOS: () =>
  {
     url = String.Format("http://maps.apple.com/maps?q={0}", address);
  },
  Android: () =>
  {
    url = String.Format("http://maps.google.com/maps?q={0}", address);
  });

Device.OpenUri(url);
like image 138
Jason Avatar answered Dec 30 '25 04:12

Jason


As of Xamarin.Forms v4.3.0.908675 (probably v4.3.0) Device.OpenUri is deprecated and you should use Launcher.TryOpenAsync from Xamarin.Essentials instead. Although you will find it troublesome, or at least I did.

like image 34
s3c Avatar answered Dec 30 '25 04:12

s3c



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!