How to check if a sandboxed app already has the access permission to a URL

I want to check whether a sandboxed application already has access permission to a specific URL.

Based on my investigation, the following FileManager method seems to be able to determine it:

FileManager.default.isReadableFile(atPath: fileURL.path)

However, the method name and description don't explicitly mention this use case, so I'm not confident there aren't any oversights.

Also, since this method takes a String path rather than a URL, I'd like to know if there's a more modern API available.

I want to use this information to decide whether to prompt the user about the Sandbox restriction in my AppKit-based app.

Answered by Etresoft in 865965022

You can use the "isReadable" value from URLResourceValues.

However, it sounds like you're not approaching this from the right direction. You should never need to do this in the first place. Your app shouldn't attempt to read random files. It should only attempt to access files that the user has specifically requested. And even then, you shouldn't check readability. That's not reliable. Instead, just try to do what the user asked and report an error if it fails.

Accepted Answer

You can use the "isReadable" value from URLResourceValues.

However, it sounds like you're not approaching this from the right direction. You should never need to do this in the first place. Your app shouldn't attempt to read random files. It should only attempt to access files that the user has specifically requested. And even then, you shouldn't check readability. That's not reliable. Instead, just try to do what the user asked and report an error if it fails.

@Etresoft

You can use the "isReadable" value from URLResourceValues.

Thank you. This is exactly the API I was looking for.

However, it sounds like you're not approaching this from the right direction.

I'm not trying to open random files...; I just want to use it to prompt the user to obtain access permissions if they don't have them for the file they tried to open (the file linked by an alias).

While isReadable will fine, I find that the checkResourceIsReachable() method is easier to use as a drop-in replacement. [See correction below.]

FYI, all of these boil down to the access system call; see its man page for details.

Share and Enjoy

Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"

@DTS Engineer Hi Quinn, thank you for additional information.

However, I suppose checkResourceIsReachable() returns true if the file exists, regardless of whether it can be accessed. What I want to know this time is whether the app can open that file in a sandbox environment.

Oh, yeah. Sorry about the bum steer. I see so many folks ask about fileExists(atPath:) that I just went off on autopilot.

Etresoft’s suggestion of isReadable is the right answer here.

Oh, and regarding this:

all of these boil down to the access system call

It’s a good way to think about it, but it’s not literally true. The URL resource value API is primarily based on getattrlist, and it uses ATTR_CMN_USERACCESS to populate the isReadable property. That’s the same value you’d get back if you called access, but it’s using a different system call.

Share and Enjoy

Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"

How to check if a sandboxed app already has the access permission to a URL
 
 
Q