如何在 Xamarin.iOS 中使用分享

How to use Share in Xamarin.iOS

我正在关注这里的文档:

https://docs.microsoft.com/en-us/xamarin/essentials/share?tabs=android

在我的 Xamarin.iOS 应用程序中实现共享功能。我的代码:

byte[] pdf = await DownloadPdfFile();

var fn = "myfile.pdf";
var file = Path.Combine(FileSystem.CacheDirectory, fn);
await File.WriteAllBytesAsync(file, pdf);

await Share.RequestAsync(new ShareFileRequest
{
    Title = ViewModel.Parameter.Title,
    File = new ShareFile(file)
});

当我 运行 这段代码时没有任何反应。

我猜您需要在 info.plist

中添加一些权限
<key>NSPhotoLibraryAddUsageDescription</key>
<string>This app needs access to the photo gallery for saving photos and videos.</string>
<key>NSPhotoLibraryUsageDescription</key>
<string>This app needs access to photos gallery for saving photos and videos.</string>

如果您有任何疑问,请告诉我

我解决了。如前所述 here iPad 上的 运行 时存在错误,您可以通过指定 PresentationSourceBounds:

来解决此问题
await Share.RequestAsync(new ShareFileRequest
{
    Title = "My title",
    File = new ShareFile(file),
    PresentationSourceBounds = new System.Drawing.Rectangle((int)View.Frame.Width, 0, (int)(View.Frame.Width), (int)(View.Frame.Height))
});