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.
CodePudding user response:
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.