flutter for macOS : How to access "user-selected" desktop files and folders
Asked Answered
P

1

8

My setup

  • macOS Big Sur
  • Xcode 12.5.1

With macOS sandboxing, I've enabled access to

  • User selected file
  • Download folder
  • Picture folder
  • Music folder
  • Movie folder

all with Read/Write permissions.

However, when I use the official file_selector and path_provider. I still cannot reference any files and folders outside of the Downloads/Music/Movies/Pictures.

I always get

OS Error: Operation not permitted, errno = 1

whenever I tried to access a directory outside of those directories mentioned above.

Expectation

I expect that by giving User selected file permission, I should be able to allow file_selector to access say user Documents/Desktop folder. Otherwise, how could the user have "selected" files if it requires separate permission from those predefined folders?

I read from native dev posts that one would need to call the native function NSOpenPanel.

Can anyone let me know how? Is it that flutter does not support such User selected File feature?

Should I resort to the platform method channel? This sounds a bit crazy.

Plow answered 24/6, 2021 at 10:46 Comment(0)
P
19

Solved it myself based on other answers. The gist

  • App Sandboxing will forbid an app to access files/folders outside of the sandbox, except for these user folders: Downloads/Movies/Music/Pictures. For instance, you won't be able to access home folder by using
path.join(path_provider.getApplicationDocumentsDirectory(), '..');

which would just give you

OS Error: Operation not permitted, errno = 1
  • Under Sandboxing mode, only limited locations are accessible, which must be configured within the Xcode project, either through Xcode GUI or onto the entitlement files directly.

  • For internal tooling or dev purposes, disable Sandboxing is good enough. The ways to disable it are

    • Within Xcode project Signing & Capabilities panel, click the "X" button at the right end of the App Sandbox category.
    • Editing the plist files DebugProfile.entitlements and Release.entitlements by setting the field
<key>com.apple.security.app-sandbox</key>
<false/>

After this, the file_selector API will be able to access files outside of the app sandbox.

Plow answered 25/6, 2021 at 2:33 Comment(3)
Which permissions does an application need to request so the user can save files to arbitrary locations from it (including removable volumes and network volumes)? Does this also require sandboxing to be disabled? If yes and the developer doesn't want to go that far, could the app detect if the user selects a forbidden location and show a proper error message?Pirogue
I tried this today: If you set "com.apple.security.app-sandbox" in "Release.entitlements" to false your released app can write to any destination on the Mac (in user space).Roving
Hi @kakyo, I had a similar question, to access / delete files, which ends up in a errno = 13. Maybe, you know a solution? #75021470Aylmer

© 2022 - 2024 — McMap. All rights reserved.