0

When implementing "Open in.." feature via the file sharing options, app cannot access the downloaded files at "File Provider Storage", FileManager.default.fileExists(atPath: url.path) returns false although the file exists, at the same time in the system console there are logs like

Sandbox: FileAccessIssueA(11544) deny(2) file-test-existence

which may be or may be not related. Also, the app successfully opens the file directly from the AirDrop or if the file is moved to the app folder.

To reproduce, please see the sample project (use *.txt files to be opened by the app).

Please advice if this is normal behavior, and if so, please direct to the appropriate documentation. If it's not, please advice if the fix is possible.

Maksym Gontar
  • 22,765
  • 10
  • 78
  • 114
  • 1
    "app cannot access the downloaded files" & "app successfully opens the file directly if the file is moved to the app folder". So my guess is that you are trying to access files that were downloaded and saved to Files.app. In that case this link may be helpful - https://developer.apple.com/documentation/uikit/view_controllers/providing_access_to_directories – Kirow Oct 07 '21 at 11:37
  • @Kirow thank you for your help, it really resolves the issue – Maksym Gontar Oct 07 '21 at 14:39

1 Answers1

1

You're using the option Supports opening documents in place in the info.plist of your application.

When you use this option, you have to explicitly ask for access to the file from the url using security scoping, so changing the code to access the file to something kinda like:

let isSecuredURL = url.startAccessingSecurityScopedResource() == true
let result = FileManager.default.fileExists(atPath: url.path)

NSLog("File %@ %@", url.path, result ? "exists" : "not exists")

if (isSecuredURL) {
    url.stopAccessingSecurityScopedResource()
}

Will address the issue.

Anya Shenanigans
  • 91,618
  • 3
  • 107
  • 122