简体   繁体   English

iOS xCode 13 - 应用无法通过“打开方式...”共享选项访问下载的文件

[英]iOS xCode 13 - App can't access downloaded files via "Open in..." sharing option

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通过文件共享选项实现“打开方式...”功能时,应用程序无法访问“文件提供程序存储”中下载的文件, FileManager.default.fileExists(atPath: url.path)虽然文件存在,但返回false ,同时系统控制台中的时间有类似的日志

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.此外,应用程序可以直接从 AirDrop 成功打开文件,或者如果文件已移动到应用程序文件夹。

To reproduce, please see the sample project (use *.txt files to be opened by the app).要重现,请参阅示例项目(使用 *.txt 文件由应用程序打开)。

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.如果不是,请告知是否可以修复。

You're using the option Supports opening documents in place in the info.plist of your application.您在应用程序的 info.plist 中使用Supports opening documents in place选项。

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:当您使用此选项时,您必须使用安全范围明确要求访问来自 url 的文件,因此将访问文件的代码更改为类似于:

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.将解决该问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM