Xamarin
A Microsoft open-source app platform for building Android and iOS apps with .NET and C#.
5,363 questions
This browser is no longer supported.
Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.
string filePath = "/private/var/mobile/Containers/Shared/AppGroup/7E6455EB-2EF9-417E-BC79-71BA25B53853/File Provider Storage/file.pdf"
byte[] byteFile = File.ReadAllBytes(filePath);
When I run the code above on Xamarin iOS I get an unauthorized exception. Saying access to filePath is denied. Can someone please help me with this, I ran out of ideas at this point.
I added all the permissions below even created App Groups in Xcode and called it in Visual Studio Mac still I'm getting the same error message.
Please help me, I'm running out of time.
Thank you!
The answer is you simply need to call e.Url.StartAccessingSecurityScopedResource();
Then call e.Url.StopAccessingSecurityScopedResource(); when done
private void DocumentPicker_DidPickDocument(object sender, UIDocumentPickedEventArgs e)
{
var documentUrl = e.Url;
// Check if the document can be accessed (sandbox security)
bool isSecured = documentUrl.StartAccessingSecurityScopedResource();
if (isSecured)
{
// Access the document here
// Once done, release the resource
documentUrl.StopAccessingSecurityScopedResource();
}
else
{
// Handle permission denial or failure to access
Console.WriteLine("Could not access the document.");
}
}