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.
- Launcher.LaunchUriAsync( new Uri(“tamilapp:?ID=asrw6”));
- Launcher.LaunchFileAsync(commonFile);
- DataTransferManager.ShowShareUI();
There are 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 the desired 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.
- var options = new LauncherOptions();
- options.TargerApplicationPackageFamilyName = “24919.App”;
-
- var launcherUri = new Uri(“App:?AddUrl=http………com”);
- 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.
- var options = new LauncherOptions();
- options.TargerApplicationPackageFamilyName = “24919.App”;
-
- var fileToken = SharedStorageAccessManager.AddFile(TamilContext);
-
- ValueSet valueSet = new ValueSet();
- valueSet.Add(“Token”, fileToken);
-
- var launcherUri = new Uri(“App:?AddUrl=http………com”);
- await Launcher.LaunchUriAsync(launchUri, options, valueSet);
Launch for results
It's a two-way communication if a source sends some data 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.
- var myQuery = new Uri(“TamilApp:”);
- await Launcher.QueryUriSupportAsync(myQuery, LaunchUri.LaunchUri);
-
-
- var myQuery = new Uri(“TamilApp:”);
- string name = “28019.TamilApp”;
- 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
- Create a Windows Universal “Black Application” from the New Project dialog (Ctrl + Shift + N).
- Add a Button to launch a button when clicking on it using the following XAML code and C# Code.
- <Button Content=”Launch” Click=”Button_Click”/>
- private async void Button_Click(object sender, RoutedEventArgs e)
- {
- await Windows.System.Launcher.LaunchUriAsync(new Uri("iamtamilanselva:?data=12345"));
- }
- Create a dummy App to launch and add a protocol ID in the Package Manifest as in the following tags.
- <Extensions>
- <uap:Extension Catefory=”windows.protocol”>
- <uap:Protocol Name=”iamtamilanselva”>
- <uap:Logo>Assests\Logo.png</uap:Logo>
- </iap:Protocol>
- </uap:Extension>
- </Extensions>
- Add content to the mainframe using the OnActivated method in the App.xaml.cs file using the following code snippet.
- protected override void OnActivated(IActivatedEventArgs args) {
- ProtocolActivatedEventArgs paea = args as ProtocolActivatedEventArgs;
-
- if (args != null) {
- Frame content = Window.Current.Content as Frame;
-
- if (content == null) {
- content = new Frame();
-
- content.Language = Windows.Globalization.ApplicationLanguages.Languages[0];
- Window.Current.Content = content;
- }
-
- if (content.Content == null) {
- content.Navigate(typeof(MainPage), null);
-
- }
- }
-
- Window.Current.Activate();
- base.OnActivated(args);
- }
- Now deploy the dummy app that is about to launch by our main app.
- 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 the app, when more apps contain the same protocol name?
To avoid this kind of manual choosing 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.
- Windows.System.LauncherOptions opt = new Windows.System.LauncherOptions();
- opt.TargetApplicationPackageFamilyName = "5c2914c8-8190-4f3c-a064-9725339b8b14_p1rwhdjzffatw";
- 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.
- Windows.ApplicationModel.Package.Current.Id.FamilyName;
Summary
In this article, we learned about Inter-App Communications in Windows 10 UWP.