checkResourceIsReachableAndReturnError or fileExistsAtPath for security scoped resources

A security scoped bookmark can only be created for URLs that point to resources that exist.

What is the preferred/correct API to use to determine if the resource exists?

[NSFileManager fileExistsAtPath:] is more explicitly about existence, but might return NO if the app hasn't established access to the resource via an implicit or explicit startAccessingSecurityScopedResource?

[NSURL checkResourceIsReachableAndReturnError] might explicitly deal with the latter issue (?), but might also return an error for other reasons than the resource not existing (?).

Thanks!

Answered by DTS Engineer in 866760022

So that error is NSFileReadNoSuchFileError. It seems reasonable to treat that as the equivalent of -fileExistsAtPath: returning false.

Share and Enjoy

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

My standard approach would be to try creating the security-scoped bookmark and see if that fails. I’m presuming that you tried that. What went wrong?

Share and Enjoy

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

My standard approach would be to try creating the security-scoped bookmark and see if that fails. I’m presuming that you tried that. What went wrong?

Thanks Quinn! Yepp, that's the first thing I tried. It produces a console error:

Scoped bookmarks can only be created for existing files or directories
Type: Error | Timestamp: 2025-11-18 14:10:18.411061+01:00 | Process: tst_manual_sandboxed_file_access | Library: CoreServicesInternal | Subsystem: com.apple.FileURL | Category: scoped | TID: 0xdd7936

and the bookmarkDataWithOptions call fails with an NSError:

Error Domain=NSCocoaErrorDomain Code=260 "Scoped bookmarks can only be created for existing files or directories" UserInfo={NSURL=file:///Users/torarne/write-single-file2.txt, NSDebugDescription=Scoped bookmarks can only be created for existing files or directories}

Which led me to using [NSURL checkResourceIsReachableAndReturnError:] before saving the bookmark.

And in turn this forum topic to determine if that's the right approach to handle this use-case, or if fileExistsAtPath is preferred :)

My worry is that fileExistsAtPath might produce false negatives, where checkResourceIsReachableAndReturnError would have returned YES. On the other hand, checkResourceIsReachableAndReturnError is less specific of why it would fail, when it fails.

Accepted Answer

So that error is NSFileReadNoSuchFileError. It seems reasonable to treat that as the equivalent of -fileExistsAtPath: returning false.

Share and Enjoy

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

Thanks, I'll check explicitly for the combination of NSCocoaErrorDomain and NSFileReadNoSuchFileError then.

For anyone ending up in this thread, note that NSFileNoSuchFileError is not the same error as what's being reported by checkResourceIsReachableAndReturnError. The error is:

Domain=NSCocoaErrorDomain Code=260 "The file “write-single-file.txt” couldn’t be opened because there is no such file." UserInfo={NSURL=file:///Users/torarne/write-single-file.txt, NSFilePath=/Users/torarne/write-single-file.txt, NSUnderlyingError=0xa83590c90 {Error Domain=NSPOSIXErrorDomain Code=2 "No such file or directory"}} 
checkResourceIsReachableAndReturnError or fileExistsAtPath for security scoped resources
 
 
Q