Share via


SharedStorageAccessManager Class

Definition

Enables an app to share a file with another app by passing a token via Uri activation, app service, REST API, etc. The target app redeems the token to get the file shared by the source app.

public ref class SharedStorageAccessManager abstract sealed
/// [Windows.Foundation.Metadata.ContractVersion(Windows.Foundation.UniversalApiContract, 65536)]
class SharedStorageAccessManager final
[Windows.Foundation.Metadata.ContractVersion(typeof(Windows.Foundation.UniversalApiContract), 65536)]
public static class SharedStorageAccessManager
Public Class SharedStorageAccessManager
Inheritance
Object Platform::Object IInspectable SharedStorageAccessManager
Attributes

Windows requirements

Device family
Windows 10 (introduced in 10.0.10240.0)
API contract
Windows.Foundation.UniversalApiContract (introduced in v1.0)

Examples

In the following example, a source app launches a mapping app and provides a .gpx file that contains driving directions to be displayed by the target app.

First, the source app gets a token for the .gpx file and uses protocol activation to launch the target app.

public async void ShareMostRecentDirections()
{
       // Get the most recently opened .gpx file
       // from the recently used file list.
       StorageItemMostRecentlyUsedList mostRecent = 
           StorageApplicationPermissions.MostRecentlyUsedList;

       String mruToken = mostRecent.Entries.FirstOrDefault().Token;
       StorageFile file = await mostRecent.GetFileAsync(mruToken);

       // Get the token to share access to the updated .gpx file.
       String sharingToken = SharedStorageAccessManager.AddFile(file);

       //Launch the driving application .
       Uri driveTo = new Uri("nokia-drive-to:?Gpxfile=" + sharingToken);
       var launch = await Launcher.LaunchURIAsync(driveTo);
}

Next, the target app gets the .gpx file by providing the token received from the source app.

protected override async void OnActivated(IActivatedEventArgs args)
{
    var protocolArgs = args as ProtocolActivatedEventArgs;

    // Get the token from the URI.
    var queryStrings = new WwwFormUrlDecoder(protocolArgs.Uri.Query);
    string gpxFileToken = queryStrings.GetFirstValueByName("GpxFile");

    // Get the .gpx file and call a custom method
    // to display driving directions.
    if (!string.IsNullOrEmpty(gpxFileToken))
    {
        PlotGpxFile(await
            SharedStorageAccessManager.RedeemTokenForFileAsync(gpxFileToken));
    }
}

Remarks

Here is the sequence of steps that enables an app to share a file with another app by passing a token as part of a Uri activation, for example.

  • The source app calls the AddFile method to get the sharing token that it passes to the target app, which it launches with a Uri.
  • The target app calls the RedeemTokenForFileAsync method to get the shared file.
  • Optionally, the source app can call the RemoveFile method to revoke a token that it obtained previously by calling the AddFile method. For more info about Uri activation, see Launch the default app for a URI.

The use of the SharedStorageAccessManager class and of sharing tokens is subject to the following requirements and restrictions.

  • A sharing token can only be redeemed one time. After that, the token is no longer valid.
  • A sharing token expires after 14 days and is no longer valid.
  • The source app cannot get more than one thousand sharing tokens. After a token is redeemed, removed, or expires, however, it no longer counts against the quota of the source app.

Network files are not supported with this class.

Methods

AddFile(IStorageFile)

Gets the sharing token that enables an app to share the specified file with another app.

RedeemTokenForFileAsync(String)

Gets a file shared by another app by providing the sharing token received from the source app.

RemoveFile(String)

Revokes an existing sharing token.

Applies to