Windows 10 - App to App communication in Universal Windows Applications
Windows 10 provides many techniques that developers can use for inter- app communications. Publisher Cache Folders is one such technique. The idea is that A publisher can have multiple applications in Windows Store and they may share common data , settings and other artifacts that could be leveraged across multiple applications.
While using Publisher Cache Folders in your application , here are few things to keep in mind
-The apps reading/writing or using Publisher Cache Folders needs to be from same publisher.
-Shared storage location is provisioned automatically when the first app for a given publisher is installed and it will be removed automatically when the last app for a given publisher is uninstalled
-You are responsible for data management /versioning inside the Publisher Cache Folder.
-Publisher Cache folder can also live in a SD card
Implementing Publisher Cache Folders is really easy. In following example I have implemented a simple read write on a text file that is living inside Publisher Cache Folders from 2 different applications.
As a first step we will declare the Publisher Cache Folder in the application manifest of each application wanting to use the folder.
<Extensions> <Extension Category="windows.publisherCacheFolders"> <PublisherCacheFolders > <Folder Name="CommonFolder"/> </PublisherCacheFolders> </Extension>
Now folder is available for us to be utilized from different applications. Writing to the folder from App1
async void WritetoPublisherFolder() { StorageFolder SharedFolder = Windows.Storage.ApplicationData.Current.GetPublisherCacheFolder("CommonFolder"); StorageFile newFile = await SharedFolder.CreateFileAsync("SharedFile.txt", CreationCollisionOption.OpenIfExists); await FileIO.WriteTextAsync(newFile, textBox.Text.ToString()); }
Reading from Folder from App2
async void ReadFromSharedFolder() { StorageFolder SharedFolder = Windows.Storage.ApplicationData.Current.GetPublisherCacheFolder("CommonFolder"); StorageFile newFile = await SharedFolder.GetFileAsync("SharedFile.txt"); var text= await FileIO.ReadTextAsync(newFile); textBlock.Text= await FileIO.ReadTextAsync(newFile); ; }
Publisher Cache Folders opens up many scenarios for the developers building multiple applications and sharing the data among those applications.
Comments
- Anonymous
April 17, 2017
Hi Ashish, Thanks for your blog, it was a good information. If you can also let me know about how to make two uwp apps from same publisher. I am working on UWP app for IOT core device and I tried creating same test certificate with same publisher name but while the reading the file from App B then it gave me following error and looks like it does not recognize the app from same publisher.{System.UnauthorizedAccessException: Access is denied. (Exception from HRESULT: 0x80070005 (E_ACCESSDENIED)) at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at System.Runtime.CompilerServices.TaskAwaiter`1.GetResult() at TestAppB.StartupTask.d__54.MoveNext()} - Anonymous
April 17, 2017
Hi Ashish, Thanks for your blog, it was a good information. If you can also let me know about how to make two uwp apps from same publisher. I am working on UWP app for IOT core device and I tried creating same test certificate with same publisher name but while reading the file from App B then it gave me following error and looks like it does not recognize the app from same publisher.{System.UnauthorizedAccessException: Access is denied. (Exception from HRESULT: 0x80070005 (E_ACCESSDENIED)) at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at System.Runtime.CompilerServices.TaskAwaiter`1.GetResult() at TestAppB.StartupTask.d__54.MoveNext()}