I know these files exist because I selected them from a SwiftUI .fileImporter
dialog. I even made sure to call .startAccessingSecurityScopedResource()
and . stopAccessingSecurityScopedResource()
appropriately.
What boggles my mind is that this works as expected, just fine, every time:
Data(contentsOf: url)
but this returns false
every time:
FileManager.default.fileExists(atPath: url.path())
Even more bizarre, if I select a directory, this returns its contents as expected!
FileManager.default.contentsOfDirectory(at: url, includingPropertiesForKeys: nil)
What’s going on here?
This tripped me up for a good couple hours until I discovered that .path()
, despite seeming to be the designated replacement for the now-deprecated .path
, doesn’t behave the same.
.path()
percent-encodes the URL before returning its path. To get the old behavior, you have to pass it false
like this:
FileManager.default.fileExists(atPath: url.path(percentEncoded: false))
This should work as you expect