Inter App Communications in Windows 10 UWP

Introduction

Communication between apps is a very crucial need in mobile app development and it provides an excellent feature to enhance a good communication channel between apps. As we know, the Windows 8 series provides a good option for app communication. Now Windows 10 UWP (Windows Universal) comes with more enhanced features.

A brief about Inter App communications

In Windows 8 series we can communicate from one app to another app, by which the sender launches a URI that states a target app ID using “Launcher” that can transfer data, files and so on or we have an alternate way that uses DataTransferManager contracts.


  1. Launcher.LaunchUriAsync( new Uri(“tamilapp:?ID=asrw6”));  
  2. Launcher.LaunchFileAsync(commonFile);  



  1. DataTransferManager.ShowShareUI();  

There are the ways to communicate with apps in the Windows 8 series but now the improved feature in Windows 10 UWP provides a more unique and secure way to transfer files or data between Apps.



Launch a desire app

Used to send one specific publisher or app so the communication is made to specifically do something from an app to another. To invoke a specific app, use the package family name that is provided by stores that are globally unique. So using PackageFamilyName and Launcher URI can invoke an App.

  1. var options = new LauncherOptions();  
  2. options.TargerApplicationPackageFamilyName = “24919.App”;  
  3.   
  4. var launcherUri = new Uri(“App:?AddUrl=http………com”);  
  5. await Launcher.LaunchUriAsync(launchUri, options); 

Send file token, send data

Used to send a file token or data to another app. Here the file that is about to send can convert the token with a value set and can send to a target app with Launcher URI and options with PackageFamilyName.

  1. var options = new LauncherOptions();  
  2. options.TargerApplicationPackageFamilyName = “24919.App”;  
  3.   
  4. var fileToken = SharedStorageAccessManager.AddFile(TamilContext);  
  5.   
  6. ValueSet valueSet = new ValueSet();  
  7. valueSet.Add(“Token”, fileToken);  
  8.   
  9. var launcherUri = new Uri(“App:?AddUrl=http………com”);  
  10. await Launcher.LaunchUriAsync(launchUri, options, valueSet); 

Launch for results

It's a two-way communication if a source sends some date to a target, then it processes it and returns the result to the source. Here the sender can process a query on the target app.

  1. var myQuery = new Uri(“TamilApp:”);  
  2. await Launcher.QueryUriSupportAsync(myQuery, LaunchUri.LaunchUri);  
  3.   
  4. //With Family name of the package  
  5. var myQuery = new Uri(“TamilApp:”);  
  6. string name = “28019.TamilApp”;  
  7. await Launcher.QueryUriSupportAsync(myQuery, LaunchUri.LaunchUri, name); 

Apps services

It's a background process and a UI-less process launches a UI based app. It's simply a web service on the device.

Step-by-step process to launch an App from another App

  1. Create a Windows Universal “Black Application” from the New Project dialog (Ctrl + Shift + N).
  2. Add a Button to launch a button when clicking on it using the following XAML code and C# Code:
    1. <Button Content=”Launch” Click=”Button_Click”/> 
    1. private async void Button_Click(object sender, RoutedEventArgs e)  
    2. {  
    3. await Windows.System.Launcher.LaunchUriAsync(new Uri("iamtamilanselva:?data=12345"));  



  3. Create a dummy App to launch and add a protocol ID in the Package Manifest as in the following tags.
    1. <Extensions>  
    2.    <uap:Extension Catefory=”windows.protocol”>  
    3.    <uap:Protocol Name=”iamtamilanselva”>  
    4.       <uap:Logo>Assests\Logo.png</uap:Logo>  
    5.       </iap:Protocol>  
    6.    </uap:Extension>  
    7. </Extensions> 


  4. Add content to the main frame using the OnActivated method in the App.xaml.cs file using the following code snippet:
    1. protected override void OnActivated(IActivatedEventArgs args) {  
    2.     ProtocolActivatedEventArgs paea = args as ProtocolActivatedEventArgs;  
    3.   
    4.     if (args != null) {  
    5.         Frame content = Window.Current.Content as Frame;  
    6.   
    7.         if (content == null) {  
    8.             content = new Frame();  
    9.   
    10.             content.Language = Windows.Globalization.ApplicationLanguages.Languages[0];  
    11.             Window.Current.Content = content;  
    12.         }  
    13.   
    14.         if (content.Content == null) {  
    15.             content.Navigate(typeof(MainPage), null);  
    16.   
    17.         }  
    18.     }  
    19.   
    20.     Window.Current.Activate();  
    21.     base.OnActivated(args);  




  5. Now deploy the dummy app that is about to launch by our main app.



  6. Now run our main app “Uri_Launch” and click the Launch button, It launches our Dummy App “TamilAppOne”. That is:


What happens if we have two apps in the same Protocol name?

In the store there may be two apps with the same name (by coincidence). It will then allow you to choose among them. For example, I created another app named “TamilAppTwo” with the same protocol name as “iamtamilanselva” and deployed it in my local machine. Then now I run my Uri_Launch app and through that I click the launch button. It shows me as in the following screenshot that all me to choose apps.



How can I avoid manual choosing on app, when more apps contain the same protocol name?

To avoid this kind of manual choosing of an app you can use “LauncerOptions's PackageFamilyName” that provides a unique ID on an app. You can do this using the following code snippet.

  1. Windows.System.LauncherOptions opt = new Windows.System.LauncherOptions();  
  2. opt.TargetApplicationPackageFamilyName = "5c2914c8-8190-4f3c-a064-9725339b8b14_p1rwhdjzffatw";  
  3. await Windows.System.Launcher.LaunchUriAsync(new Uri("iamtamilanselva:?SomeData=123"),opt); 

How can I get my app's Package Family Name?

Using the following code snippet, you can get the current app's Package family name.

  1. Windows.ApplicationModel.Package.Current.Id.FamilyName; 

Up Next
    Ebook Download
    View all
    Learn
    View all