Recent

Author Topic: Detecting if a file is readable  (Read 5314 times)

ChrisR

  • Full Member
  • ***
  • Posts: 247
Re: Detecting if a file is readable
« Reply #15 on: July 25, 2020, 07:39:38 pm »
Jonas,
  OK, I can understand your rationale. Maybe other Mac users can find my code useful when creating their most-recently-used menus and making them aware of the feature of MacOS.

ChrisR

  • Full Member
  • ***
  • Posts: 247
Re: Detecting if a file is readable
« Reply #16 on: July 27, 2020, 04:10:52 pm »

Igor Kokarev

  • Sr. Member
  • ****
  • Posts: 370
Re: Detecting if a file is readable
« Reply #17 on: February 04, 2021, 01:57:07 pm »
How I can check if my app has permission to access files inside certain user's folders?

My app is NOT sandboxed.

In Catalina/Big Sur when my app tries to access any file inside Documents, Downloads, Desktop, etc folders, user will see a request from macOS to approve access for this folder.

I need to check a permission for a folder for my app BEFORE appearing of a request from macOS. How I can do that? I don't need to read a certain file, because my app enumerate all files in a folder to generate a preview.

trev

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 2023
  • Former Delphi 1-7, 10.2 user
Re: Detecting if a file is readable
« Reply #18 on: February 05, 2021, 07:14:48 am »
I made a note a while ago that was sourced from our Eskimo friend, but then never got back to it, that you get EACCESS if the read request fails due to file system permissions and ACLs, but that you get EPERM if the request fails due to mandatory access control (Sandbox and TCC - Privacy & Security). See Apple Docs though not very enlightening.

Igor Kokarev

  • Sr. Member
  • ****
  • Posts: 370
Re: Detecting if a file is readable
« Reply #19 on: February 05, 2021, 12:34:21 pm »
trev,

Thanks for your reply. It seems too complicated to implement this code.

trev

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 2023
  • Former Delphi 1-7, 10.2 user
Re: Detecting if a file is readable
« Reply #20 on: February 05, 2021, 12:50:19 pm »
From this Apple Article:

Quote
Check for Authorization

Many system frameworks that provide access to protected resources have dedicated APIs for checking and requesting authorization to use those resources. This allows you to adjust your app’s behavior depending on the current access it has. For example, if the user denies your app permission to do something, you can remove related elements from your user interface. Because a user can change authorization at any time using Settings, always check the authorization status of a feature before accessing it. In cases without a dedicated API, be prepared to gracefully handle access failures.

Unfortunately you have to go hunting for the relevant API eg

Quote
Declaration
class func authorizationStatus(for mediaType: AVMediaType) -> AVAuthorizationStatus
Parameters
mediaType
A media type constant, either video or audio.
Return Value
A constant indicating authorization status.

I guess this is the "official" way to check access to those protected resources (documents folder etc).

Don't forget that if you use, for example, an open dialog to access a file, intent is implied and the user does not get the request for access permission.

Igor Kokarev

  • Sr. Member
  • ****
  • Posts: 370
Re: Detecting if a file is readable
« Reply #21 on: February 05, 2021, 01:22:17 pm »
trev,

Thanks for this additional info!

 

TinyPortal © 2005-2018