How to launch the default app for a URI (HTML)

Learn how to launch the default app for a Uniform Resource Identifier (URI). URIs allow you to launch another app on the operating 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.

These steps show how to use the Windows.System.Launcher API to launch the default handler for a URI.

Instructions

Step 1: Create the URI

Create a Windows.Foundation.Uri object for the URI to launch. This URI uses the http scheme name.


// The URI to launch
var uriToLaunch = "https://www.bing.com";

// Create a Uri object from a URI string 
var uri = new Windows.Foundation.Uri(uriToLaunch);

Step 2: Launch the URI

The operating system provides several different options for launching the default handler for a URI. These options are described in this chart and in the sections that follow.

Option Method Description
Default launch LaunchUriAsync(Uri) Launch the specified URI with the default handler.
Launch with a warning dialog LaunchUriAsync(Uri, LauncherOptions) The operating system shows 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 store to the user.
Launch with a desired remaining view LaunchUriAsync(Uri, LauncherOptions) (Windows-only) Launch the specified URI with the default handler. Specify a preference to stay on screen after the launch and request a specific window size.

Windows 8.1:  LauncherOptions.DesiredRemainingView isn't supported until Windows 8.1 and Windows Server 2012 R2.

Windows Phone:  LauncherOptions.DesiredRemainingView isn't supported for Windows Phone.

 

These examples use the Windows.System.Launcher.launchUriAsync method to launch the URI. This is an overloaded method.

Default launch

Call the Windows.System.Launcher.launchUriAsync(Uri) method to launch the URI created in step 1 using the default app for the http URI.

// Launch the URI
Windows.System.Launcher.launchUriAsync(uri).then(   
   function (success) {
      if (success) {
        // URI launched
      } else {
        // URI launch failed
      }
   });

Launch with a warning dialog

Call the Windows.System.Launcher.launchUriAsync(Uri, LauncherOptions) method to launch the URI created in step 1 with a warning. Use the treatAsUntrusted property to indicate that the operating system display a warning.

Note  

Call preventDefault in your event handler if the treatAsUntrusted property is set and you are using an a element to launch the URI.

 

A warning dialog overlayed on a grayed out background of the app. The dialog asks the user if they want to switch apps and has ‘Yes’ and ‘No’ buttons in the bottom right. The ‘No’ button is highlighted.

function linkClickHandler(eventInfo) {
    var link = eventInfo.target;
    if (eventInfo.srcElement && (
        (eventInfo.type === "click") ||
        (eventInfo.type === "keydown" && (
        eventInfo.keyCode === WinJS.Utilities.Key.enter ||
        eventInfo.keyCode === WinJS.Utilities.Key.space)))) {
        eventInfo.preventDefault();
        if (link.href.indexOf("ms-appx") > -1) {
            WinJS.Navigation.navigate(link.href);
        }
        else if (link.href.indexOf("http") > -1) {
            // Create a Uri object from a URI string 
            var uri = new Windows.Foundation.Uri(link.href);
            var options = new Windows.System.LauncherOptions();
            // Launch the URI with a warning prompt
            options.treatAsUntrusted = true;
            // Launch the URI
            Windows.System.Launcher.launchUriAsync(uri, options).then(
                function (success) {
                    if (success) {
                        // URI launched
                    } else {
                        // URI launch failed
                    }
                });
        }
    }
}

Launch with a recommended app fallback

In some cases, the user might not have an app installed to handle the URI that you are launching. By default, the operating system handles these cases by providing the user with a link to search for an appropriate app on the store. If you want to give the user a specific recommendation for which app to acquire in this scenario, you can do so by passing that recommendation along with the file that you are launching. To do this, call the Windows.System.Launcher.LaunchUriAsync(Uri, 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. The operating system uses this info 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.

 

The Open With dialog for a contoso URI launch. Since contoso does not have a handler installed on the machine the dialog contains an option with the Store icon and text which points the user to the correct handler on the Store. The dialog also contains a ‘More options’ link.

// Set the recommended app.
var options = new 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
Windows.System.Launcher.launchUriAsync(uri, options).then(
   function (success) {
      if (success) {
        // Uri launched
      } else {
        // Uri launch failed
      }
   });

Launch with a Desired Remaining View (Windows-only)

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 operating 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 doesn't 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.

 

Windows 8.1: LauncherOptions.DesiredRemainingView isn't supported until Windows 8.1 and Windows Server 2012 R2.

Windows Phone: LauncherOptions.DesiredRemainingView isn't supported for Windows Phone.

// Launch the URI with a desired remaining view
var options = new Windows.System.LauncherOptions();
options.desiredRemainingView = Windows.UI.ViewManagement.ViewSizePreference.useLess;

Windows.System.Launcher.launchUriAsync(uri, options).then(
   function (success) {
      if (success) {
        // URI launched
      } else {
        // URI launch failed
      }
   });

Remarks

Your app cannot select the app that is launched. The user determines which app is launched. The user can select either a 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 must always take some action to initiate a URI launch. If you attempt to launch a URI and your app isn't 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, for example, a file:/// URI that points to a network location.

You can't use this method to launch a URI in the local zone. For example, apps can't use the file:/// URI to access files on the local device. 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.

Complete example

See Association launching sample (Windows).

Tasks

How to handle protocol activation

How to launch the default app for a file

Guidelines

Guidelines and checklist for file types and URIs

Reference

Windows.System.Launcher.launchUriAsync