Search code examples
c#xamarincross-platformxamarin.forms

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.


Solution

  • 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);