Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix IOSStorageFolder permission problem #13976

Merged
merged 2 commits into from
Dec 22, 2023

Conversation

sghctoma
Copy link
Contributor

What does the pull request do?

This PR aims to fix StorageProvider.OpenFolderPickerAsync permission errors when run on an iOS device.

What is the current behavior?

Folders opened using StorageProvider.OpenFolderPickerAsync can't be used on an iPhone, because of "Operation not permitted" errors on every operation.

OpenFolderPickerAsync-error

Please note that these errors occur only on a real device, the emulator works without the changes in this PR. My test device runs iOS 17.1.2, and I do not have access to any other Apple devices / iOS versions to test on.

I have attached a minimal test application to demonstrate this behavior. The application asks the user for a folder, and puts the name of all files and folders under it into a ListBox. The file/folder list will appear in the ListBox when run in the emulator, but not when run on a real device.

What is the updated/expected behavior with this PR?

With the commit in this PR, folders opened using StorageProvider.OpenFolderPickerAsync can be accessed as normal.

How was the solution implemented (if it's not obvious)?

The root cause of the problem is that IOSStorageFolder does not call StartAccessingSecurityScopedResource / StopAccessingSecurityScopedResource on its Url. This PR adds these calls to IOSStorageFolder's constructor and Dispose methods, similarly to IOSSecurityScopedStream.

@sghctoma
Copy link
Contributor Author

@dotnet-policy-service agree

@avaloniaui-bot
Copy link

You can test this PR using the following package version. 11.1.999-cibuild0043010-beta. (feed url: https://nuget-feed-all.avaloniaui.net/v3/index.json) [PRBUILDID]

This commit fixes "Operation not permitted" errors when accessing
folders opened via StorageProvider.OpenFolderPickerAsync by calling
[Start/Stop]AccessingSecurityScopedResource on IOSStorageFolder's
Url.
Wrap individual operations in [Start/Stop]AccessingSecurityScopedResource
calls instead of retaining access for the whole lifetime of the folder.
@avaloniaui-bot
Copy link

You can test this PR using the following package version. 11.1.999-cibuild0043113-beta. (feed url: https://nuget-feed-all.avaloniaui.net/v3/index.json) [PRBUILDID]

Copy link
Member

@maxkatz6 maxkatz6 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@maxkatz6 maxkatz6 added the backport-candidate-11.0.x Consider this PR for backporting to 11.0 branch label Dec 22, 2023
@maxkatz6 maxkatz6 added this pull request to the merge queue Dec 22, 2023
Merged via the queue into AvaloniaUI:master with commit 10b203b Dec 22, 2023
6 checks passed
sghctoma added a commit to sghctoma/Sufni.Bridge that referenced this pull request Dec 22, 2023
This contains the IOSStorageFolder permission fixes: AvaloniaUI/Avalonia#13976
@maxkatz6 maxkatz6 added backported-11.0.x and removed backport-candidate-11.0.x Consider this PR for backporting to 11.0 branch labels Jan 17, 2024
maxkatz6 pushed a commit that referenced this pull request Jan 17, 2024
* Fix IOSStorageFolder permission problem

This commit fixes "Operation not permitted" errors when accessing
folders opened via StorageProvider.OpenFolderPickerAsync by calling
[Start/Stop]AccessingSecurityScopedResource on IOSStorageFolder's
Url.

* Narrow down security scoped resource access scope

Wrap individual operations in [Start/Stop]AccessingSecurityScopedResource
calls instead of retaining access for the whole lifetime of the folder.
#Conflicts:
#	src/iOS/Avalonia.iOS/Storage/IOSStorageItem.cs
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants