This article describes how you can use the .NET Multi-platform App UI (.NET MAUI) IShare interface. This interface provides an API to send data, such as text or web links, to the devices share function.
The default implementation of the IShare interface is available through the Share.Default property. Both the IShare interface and Share class are contained in the Microsoft.Maui.ApplicationModel.DataTransfer namespace.
When a share request is made, the device displays a share window, prompting the user to choose an app to share with:
Get started
To access the Share functionality, the following platform-specific setup is required:
If your application is going to share media files, such as photos and videos, you must add the following keys to your Platforms/iOS/Info.plist and Platforms/MacCatalyst/Info.plist files:
<key>NSPhotoLibraryAddUsageDescription</key>
<string>This app needs access to the photo gallery to save photos and videos.</string>
<key>NSPhotoLibraryUsageDescription</key>
<string>This app needs access to the photo gallery to save photos and videos.</string>
The <string> elements represent the text shown to your users when permission is requested. Make sure that you change the text to something specific to your application.
No setup is required.
Share text and links
The share functionality works by calling the RequestAsync method with a data payload that includes information to share to other applications. ShareTextRequest.Text and ShareTextRequest.Uri can be mixed and each platform will handle filtering based on content.
public async Task ShareText(string text)
{
await Share.Default.RequestAsync(new ShareTextRequest
{
Text = text,
Title = "Share Text"
});
}
public async Task ShareUri(string uri, IShare share)
{
await share.RequestAsync(new ShareTextRequest
{
Uri = uri,
Title = "Share Web Link"
});
}
Share a file
You can also share files to other applications on the device. .NET MAUI automatically detects the file type (MIME) and requests a share. However, operating systems may restrict which types of files can be shared. To share a single file, use the ShareFileRequest type.
The following code example writes a text file to the device, and then requests to share it:
public async Task ShareFile()
{
string fn = "Attachment.txt";
string file = Path.Combine(FileSystem.CacheDirectory, fn);
File.WriteAllText(file, "Hello World");
await Share.Default.RequestAsync(new ShareFileRequest
{
Title = "Share text file",
File = new ShareFile(file)
});
}
Share multiple files
Sharing multiple files is slightly different from sharing a single file. To share a single file, use the ShareMultipleFilesRequest type.
The following code example writes two text files to the device, and then requests to share them:
public async Task ShareMultipleFiles()
{
string file1 = Path.Combine(FileSystem.CacheDirectory, "Attachment1.txt");
string file2 = Path.Combine(FileSystem.CacheDirectory, "Attachment2.txt");
File.WriteAllText(file1, "Content 1");
File.WriteAllText(file2, "Content 2");
await Share.Default.RequestAsync(new ShareMultipleFilesRequest
{
Title = "Share multiple files",
Files = new List<ShareFile> { new ShareFile(file1), new ShareFile(file2) }
});
}
Control file locations
Important
This section only applies to Android.
In some scenarios on Android, such as when a file is in private storage, it can be copied into the app cache which is then shared via an Android FileProvider. However, this can unintentionally expose the entire cache and app data to an attacker. This can be prevented by adding a file provider file paths override file to your app, and ensuring that files are copied to the location specified in this file prior to sharing.
To add a file provider file paths override file to your app, add a file named microsoft_maui_essentials_fileprovider_file_paths.xml to the Platforms\Android\Resources\xml folder in your app. Therefore, the full relative file name to the project should be Platforms\Android\Resources\xml\microsoft_maui_essentials_fileprovider_file_paths.xml. Then, add XML to the file for your required paths:
For more information about file provider paths, see FileProvider on developer.android.com.
Prior to sharing a file, you should ensure it's first written to the sharing-root folder in one of the locations from the override file:
// Write into the specific sub-directory
var dir = Path.Combine(FileSystem.CacheDirectory, "sharing-root");
Directory.CreateDirectory(dir);
var file = Path.Combine(dir, "mydata.txt");
await File.WriteAllTextAsync(file, $"My data: {count}");
// Share the file
await Launcher.OpenAsync(new OpenFileRequest
{
Title = "My data",
File = new ReadOnlyFile(file),
});
You can verify that the file is being shared correctly if the shared URI excludes the sharing root directory. For example, if you share the file <CacheDirectory>/sharing-root/mydata.txt and the shared URI is content://com.companyname.overwritefileproviderpaths.fileProvider/internal_cache/sharing-root/mydata.txt then the file provider isn't using the correct path. If the shared URI is content://com.companyname.overwritefileproviderpaths.fileProvider/internal_cache/mydata.txt then the file provider is using the correct path.
Warning
When sharing a file, if you receive an Java.Lang.IllegalArgumentException, with a message similar to "Failed to find configured root that contains /data/data/com.companyname.overwritefileproviderpaths/cache/some-non-sharing-path/mydata.txt", you are most likely sharing a file that's outside of the sharing-root.
Presentation location
Important
This section only applies to iPadOS.
When requesting a share or opening launcher on iPadOS, you can present it in a popover. This specifies where the popover will appear and point an arrow directly to. This location is often the control that launched the action. You can specify the location using the PresentationSourceBounds property:
await Share.RequestAsync(new ShareFileRequest
{
Title = Title,
File = new ShareFile(file),
PresentationSourceBounds = DeviceInfo.Platform == DevicePlatform.iOS && DeviceInfo.Idiom == DeviceIdiom.Tablet
? new Rect(0, 20, 0, 0)
: Rect.Zero
});
The source for this content can be found on GitHub, where you can also create and review issues and pull requests. For more information, see our contributor guide.
.NET MAUI feedback
.NET MAUI is an open source project. Select a link to provide feedback:
Learn how to use static and dynamic shared resources to build a .NET Multi-platform App UI (MAUI) user interface. And see how styles can make the user interface both consistent and accessible.