0

I'm implementing sharing target in my Xamarin.iOS project.

All the described behavior works on simulator, but fails on real device.

The OpenReadAsync function is already used and working when I select the file via the FilePicker (file type is FileResult):

var file = await FilePicker.PickAsync(allowedFileTypes);
using Stream stream = await file.OpenReadAsync();

But if I try to create the FileResult manually with the path I get from the sharing target:

FileResult file = new FileResult(filePath);
using Stream stream = await file.OpenReadAsync();

I get those 2 exceptions:

  • Shared from gallery:
{System.IO.DirectoryNotFoundException: Could not find a part of the path "/var/mobile/Media/PhotoData/OutgoingTemp/EF8C049F-C529-44DB-B6FD-3CC6B3ED189D/IMG_0003.JPG".
  at System.IO.FileStream..ctor (System.String path, System.IO.FileMode mode, System.IO.FileAc…}
  • Shared from Files:
{System.UnauthorizedAccessException: Access to the path "/private/var/mobile/Containers/Shared/AppGroup/0B20B6FA-C27F-4487-9719-D877CAD58452/File Provider Storage/Downloads/IMG_0003.JPG" is denied.
  at System.IO.FileStream..ctor (System.String path, System.IO.…}

Same behavior if I use System.IO.File.ReadAllBytes(filePath)

The manually created file obviously miss some access right and the simulator isn't that restrictive as a real device.

But I cannot figure out how to read bytes from a file from just the path given by the sharing extension.

I also tried doing the conversion to bytes on the iOS side using

NSUrl nSUrl = new NSUrl(cleanUri);
nSUrl.StartAccessingSecurityScopedResource();
var data = NSData.FromUrl(nSUrl);

But NSData.FromUrl always returns null.

Grounch
  • 375
  • 2
  • 14
  • when you use the `FilePicker`, the user is explicitly granting your app the rights to access the file – Jason Aug 22 '23 at 21:46
  • So how does apps like whatsapp etc work with sharing target without asking for rights very time? My app does ask for photo access with the photo filepicker once, but it's not changing sharing behavior – Grounch Aug 23 '23 at 05:35

0 Answers0