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.
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);
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.
Use Below code, and add namespaces Xamarin.Essentials
and Xamarin.Forms
if (Device.RuntimePlatform == Device.iOS)
{
// https://developer.apple.com/library/ios/featuredarticles/iPhoneURLScheme_Reference/MapLinks/MapLinks.html
await Launcher.OpenAsync("http://maps.apple.com/?daddr=San+Francisco,+CA&saddr=cupertino");
}
else if (Device.RuntimePlatform == Device.Android)
{
// opens the 'task chooser' so the user can pick Maps, Chrome or other mapping app
await Launcher.OpenAsync("http://maps.google.com/?daddr=San+Francisco,+CA&saddr=Mountain+View");
}
else if (Device.RuntimePlatform == Device.UWP)
{
await Launcher.OpenAsync("bingmaps:?rtp=adr.394 Pacific Ave San Francisco CA~adr.One Microsoft Way Redmond WA 98052");
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.