.NET Zone is brought to you in partnership with:

Gil Fink, Microsoft MVP, is an expert in Web development and Microsoft data platform. He works as a senior architect at Sela Group. He is currently consulting for various enterprises and companies, where he architects and develops Web and RIA-based solutions. He conducts lectures and workshops for developers and enterprises who want to specialize in infrastructure and Web development. He is also a co-author of several Microsoft Official Courses and training kits. You can read his publications at his blog: http://blogs.microsoft.co.il/blogs/gilf. Gil is a DZone MVB and is not an employee of DZone and has posted 151 posts at DZone. You can read more from them at their website. View Full User Profile

Launching URIs in a Windows Store App

12.31.2012
| 3150 views |
  • submit to reddit

During the writing of a Windows Store app, we got a requirement to launch a web page from the app. Launching a web page means opening a browser with a given URI. So how can you accomplish that? The answer is very simple and is explained in this post.

The Windows.System.Launcher Namespace

In Windows Store apps you can use the WinRT Windows.System.Launcher namespace to launch web pages or files. The namespace includes the following API functions:

  • launchUriAsync – receives a Windows.Foundation.Uri and open it in the app associated with the URI scheme (in most cases a browser).
  • launchFileAsync – receives a IStorageFile and start it using the default app that is associated with the file type.

The following example shows how to launch a URI:

Windows.System.Launcher.launchUriAsync(linkURI);

Both of the functions can receive an additional LauncherOptions parameter. The LauncherOptions can help to configure the launch with options like content type or display application picker. Here is a simple example that will open the application picker:

var options = new Windows.System.LauncherOptions();
options.displayApplicationPicker = true;

Windows.System.Launcher.launchUriAsync(uri, options);

Launching a URI

Formatting a URI String In the example I’m going to use a string format function. The function receives string arguments and replace placeholders, represented with curly brackets and an argument index number, with the associated arguments. The format function is added to the string prototype and it is very useful in other situations:

String.prototype.format = function () {
    var str = this;
    for (var i = 0; i < arguments.length; i++) {
        var reg = new RegExp("\\{" + i + "\\}", "gm");
        str = str.replace(reg, arguments[i]);
    }

    return str;
};

Launching a formatted URI

In the example I’m using the following URI:

var uri = https://www.google.co.il/search?q={0};

I could have used string concatenation to create the URI but in real world scenarios you are probably going to format a more sophisticated URI so I decided to use the format function instead. 
Here is the code to launch a search in Google:

function launch(query) {
    var queryUri = getQueryUri(query);
    launcher.launchUriAsync(queryUri);
};
 
function getQueryUri(query) {
    var formattedUri = uri.format(query);
    return new Windows.Foundation.Uri(formattedUri);
};

The Full Example

Here is the entire code for a launchManager:

(function () {
    "use strict";
 
    var launcher = Windows.System.Launcher;
    var uri = "https://www.google.co.il/search?q={0}";
 
    String.prototype.format = function () {
        var str = this;
        for (var i = 0; i < arguments.length; i++) {
            var reg = new RegExp("\\{" + i + "\\}", "gm");
            str = str.replace(reg, arguments[i]);
        }
 
        return str;
    };
 
    var launcherManager = WinJS.Class.define(function () {
        var self = this;
 
        self.launch = function (query) {
            var queryUri = getQueryUri(query);
            launcher.launchUriAsync(queryUri);
        };
 
        function getQueryUri(query) {
            var formattedUri = uri.format(query);
            return new Windows.Foundation.Uri(formattedUri);
        };
    },
    {
    });
 
    WinJS.Namespace.define("Launcher", {
        launcherManager: new launcherManager()
    });
}());

For the simplicity of the example I’ve put the string format function inside of the scope of the Launcher namespace. In a real app the string format function should exist in a helpers JavaScript file.

Now you can use the Launcher namespace like in the following example:
Launcher.launcherManager.launch("HTML5");
Summary

In order to launch files or web pages you can use the Windows.System.Launcher namespace. In the post I showed how to launch an app associated with a given URI scheme.

Published at DZone with permission of Gil Fink, author and DZone MVB. (source)

(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)