In this post I want to discuss how to establish communications between Universal Windows Platform applications. It was a problem since Windows 8 because each application is running in its own sandbox without access to data of other applications. Of course, Windows 8.x supports several ways to invoke external applications and pass data to them. For example you could use share charm or use Launcher class. But all these methods have some disadvantages. Let’s look at Launcher class.
In Windows 8.x Launcher class allows to launch external applications based on Uri. The most common protocol for Uri is http and you can use it to launch browser but applications can register their own
protocols for own needs. For example, in Windows 10 to invoke Settings window you can use ms-settings protocol and create Uri for Launcher class based on it:
await Launcher.LaunchUriAsync(new Uri("ms-settings:"));
You can see that in order to launch external application you should call LaunchUriAsync method which doesn’t allow to pass anything except Uri. If you need to pass some parameters to external application, you could use the same approach as for http protocol: get parameters in Uri. But if you need to pass file as a parameter you cannot make it using LaunchUriAsync. Frankly speaking, Launcher class supports LaunchFileAsync method but that method supports StorageFile as a parameter. So, you can not pass several files and you can not combine both methods (launch application using Uri and pass file at the same time). Even in order to pass file to a(??) third-party application it is required to register extensions of accepted files. Additionally, LaunchUriAsync and LaunchFileAsync don’t allow to control which application should be launched. If several applications register the same extension, user needs to select an application from the list. Finally, there was no way to understand if an application is launched and how to get some result back.
Let’s summarize all disadvantages, which Launcher class has in Windows 8.x:
- Allows to pass Uri or file using two different methods without a way to combine them;
- Doesn’t allow to pass several files;
- System might ask user to select an application from the list;
- There is no way to know if an external application is launched;
- There is no way to get response from an external application;
But in Universal Windows Platform, Microsoft made huge investment to Launcher class and today developers can avoid all mentioned disadvantages. Let’s see which changes were implemented there.
Look at the following code:
LauncherOptions options = new LauncherOptions()
TargetApplicationPackageFamilyName = "Microsoft.MicrosoftEdge_8wekyb3d8bbwe"
await Launcher.LaunchUriAsync(new Uri("http://www.microsoft.com"), options);
When running this code we asked Launcher to launch an application which has defined application package name. This approach is very useful for corporate systems when you developed for customers more than one application. Using LauncherOptions you can guarantee that system will run exactly the application you need from your bunch of applications. Of course, you need to know the application family name but you can easily to find it using the Store:
Additionally, you can use FindUriSchemeHandlersAsync method of Launcher class to get information about all packages which accept the selected schema:
var res = await Launcher.FindUriSchemeHandlersAsync("http");
This method returns an array which contains all needed information including package name:
Of course, if package doesn’t exist in the system, LaunchUriAsync method will return false and you can ask users to install an additional application.
Launcher class contains one more method, QueryUriSupportAsync, which allows to get information if selected Uri is supported in the system:
var res = await Launcher.QueryUriSupportAsync(new Uri("http://www.microsoft.com"), LaunchQuerySupportType.Uri);
This method doesn’t return any information about external applications but allows to check if you can run an application using a passed Uri. And what is more important, this method allows to check result using package family name and even to understand if external application can return a response.
I would like to note that you can use the same bunch of methods for open files using external applications: LaunchFileAsync, FindFileHandlersAsync, QueryFileSupportAsync. Of course, these methods don’t resolve the problem with multiply files but UWP brings an opportunity to use LaunchUriAsync method for passing several files (references) as parameters. Let’s see how to implement it.
The idea is in using of SharedStorageAccessManager class. Thanks to this class you can share files between applications using tokens.
Because token is a string, you can use it as a parameter in Uri. So, you should not pass any IStorageFile objects or anything special – just the same Uri. And it is possible to create as many tokens as needed.
External application can redeem tokens and get access to IStorageFile objects:
string myFileToken = queryStrings.GetFirstValueByName("GpxFile");
StorageFile file=await SharedStorageAccessManager.RedeemTokenForFileAsync(myFileToken));
Once the token is redeemed nobody can redeem the token once again. But the token may live for 14 days. So, if the application discovers a problem with launching of an external application, there is a way to delete token from the list using RemoveFile method.
Of course, in case of file token it’s easy to include tokens to Uri but you can pass any serializable objects what you want. In order to do it you can use ValueSet class , which is a dictionary of serializable objects. Developers can use it to pass tokens as well but it’s possible to pass anything:
ValueSet v = new ValueSet();
var f = await Launcher.LaunchUriAsync(myUri, options, v);
Finally, Launcher class allows to launch Uri for results. Imagine an application for making a payment. You can use this application as external method to make payments inside your own application but you need to get information from external one if payment is processed and, probably, some information to check if payment is received. In Windows 10 you can implement it using LaunchUriForResultsAsync method. This method has the same parameter list like LaunchUriAsync but it returns LaunchUriResult object instead of bool and you can use this object to see status and get results if launch was succeeded.
var result = await Windows.System.Launcher.LaunchUriForResultsAsync(myUri, options, inputData);
if (result.Status == LaunchUriStatus.Success)
ValueSet theValues = result.Result;
//do something here
Therefore, all Windows 8.x problems with Launcher class are gone and today you have a great way to make communications between different applications.