How to launch the default app for a URI (Windows Store apps using C#/VB/C++ and XAML)
Learn how to launch the default app for a URI. URIs allow you to launch another app on the system to perform a specific task. For example, if you want to allow the user to send a mail to a contact in your app you can use the mailto: URI to launch the user’s default e-mail app.
The following steps will show you how to use the Windows.System.Launcher API to launch the default handler for a URI.
Windows provides several different options for launching the default handler for a URI. These are described in the following chart and in the sections below.
|Default launch||LaunchUriAsync(Uri)||Launch the specified Uri with the default handler.|
|Launch with a warning dialog||LaunchUriAsync (Uri, LauncherOptions)||Windows will show a warning dialog before launching the specified uri.|
|Launch with a recommended app fallback||LaunchUriAsync (Uri, LauncherOptions)||Launch the specified Uri with the default handler. If no handler is installed on the system recommend an app in the Windows Store to the user.|
|Launch with a desired remaining view||LaunchUriAsync (Uri, LauncherOptions)||Launch the specified URI with the default handler. Specify a preference to stay on screen after the launch and request a specific windows size.|
These examples use the Windows.System.Launcher.LaunchUriAsync method to launch the URI. This is an overloaded method.
Call LaunchUriAsync(Uri) to launch the URI created in step 1 using the default app for the http URI scheme.
async Sub DefaultLaunch() ' Launch the URI Dim success = await Windows.System.Launcher.LaunchUriAsync(uri) If success Then ' URI launched Else ' URI launch failed End If End Sub
Launch with a warning dialog
async Sub DefaultLaunch() ' Set the option to show a warning Dim options = Windows.System.LauncherOptions() options.TreatAsUntrusted = True ' Launch the URI with a warning prompt Dim success = await Windows.System.Launcher.LaunchUriAsync(uri, options) If success Then ' URI launched Else ' URI launch failed End If End Sub
Launch with a recommended app fallback
In some cases the user may not have an app installed to handle the URI that you are launching. By default Windows will handle these cases by providing the user with a link to search for an appropriate app on the Store. If you would like to give the user a specific recommendation for which app to acquire in this scenario you may do so by passing that recommendation along with the URI you are launching. To do this, call the Windows.System.Launcher.launchFileAsync(IStorageFile, LauncherOptions) method with LauncherOptions.preferredApplicationPackageFamilyName set to the package family name of the app in the Store that you want to recommend. Then set the LauncherOptions.preferredApplicationDisplayName to the name of that app. Windows will use this information to replace the general option to search for an app in the store with a specific option to acquire the recommended app from the Store.
Note you must set both of these options to recommend an app. Setting one without the other will result in a failure.
async Sub DefaultLaunch() ' Set the recommended app Dim options = Windows.System.LauncherOptions() options.PreferredApplicationPackageFamilyName = "Contoso.URIApp_8wknc82po1e"; options.PreferredApplicationDisplayName = "Contoso URI App"; ' Launch the URI and pass in the recommended app ' in case the user has no apps installed to handle the URI Dim success = await Windows.System.Launcher.LaunchUriAsync(uri, options) If success Then ' URI launched Else ' URI launch failed End If End Sub
Launch with a Desired Remaining View
Source apps that call LaunchUriAsync can request that they remain on screen after a URI launch. By default, Windows attempts to share all available space equally between the source app and the target app that handles the URI. Source apps can use the DesiredRemainingView property to indicate to the system that they prefer their app window to take up more or less of the available space. DesiredRemainingView can also be used to indicate that the source app does not need to remain on screen after the URI launch and can be completely replaced by the target app. This property only specifies the preferred window size of the calling app. It doesn't specify the behavior of other apps that may happen to also be on screen at the same time.
Note Windows takes into account multiple different factors when it determines the source app's final window size, for example, the preference of the source app, the number of apps on screen, the screen orientation, and so on. By setting DesiredRemainingView, you aren't guaranteed a specific windowing behavior for the source app.
Your app cannot select the app that is launched. The user determines which app is launched. The user can select any Windows Store app or a desktop app.
When launching a URI your app must be the foreground app, that is, it must be visible to the user. This requirement helps ensure that the user remains in control. To meet this requirement make sure that you tie all URI launches directly to the UI of your app. The user should always have to take some action to initiate a URI launch. If you attempt to launch a URI and your app is not in the foreground the launch will fail and your error callback will be invoked.
You must specify the privateNetworkClientServer capability in order to launch intranet URIs. You cannot use this method to launch a URI in the local zone. For example, apps cannot use the file:/// URI to access files on the local computer. Instead, you must use the Storage APIs to access files.
You cannot use this method to launch a URI in the local zone. For example, apps cannot use the file:/// URI to access files on the local computer. Instead, you must use the Storage APIs to access files. If you attempt to launch an intranet URI without the correct capability or a local zone URI the launch will fail and your error callback will be invoked.
- How to handle URI activation
- How to launch the default app for a file
- Guidelines and checklist for file types and URIs