FinderSync Extension runtime error: The file couldn’t be opened because you don’t have permission to view it

FinderSync Extension runtime error: The file couldn’t be opened because you don’t have permission to view it

我正在编写一个 FinderSync 扩展程序,用于在上下文菜单中放置一个项目。当我 right-click 时,我希望菜单项的标题根据所选文件的内容而改变。但是,当我尝试使用 Data(contentsOf: selectedUrl) 读取内容时,它会抛出以下错误:

Error Domain=NSCocoaErrorDomain Code=257 "The file “Some File.png” couldn’t be opened because you don’t have permission to view it." UserInfo={NSFilePath=/Users/Shared/MySyncExtension Documents/Some File.png, NSUnderlyingError=0x61800004cf00 {Error Domain=NSPOSIXErrorDomain Code=1 "Operation not permitted"}}

如果我添加或删除沙盒,这不会改变。


先前的研究

问题是沙盒阻止文件访问文件,因此您的应用程序无法打开它。

Read and Write access for FinderSync extension in a sandboxed environment

即使 Finder Sync App Extension 被授予 "User Selected File" 沙箱文件访问权限,用户通过右键单击访问的文件似乎不算 "user-selected".

可能会将用户引导至 select a root directory via an NSOpenPanel via Powerbox, after which all subdirectories can be accessed via a security-scoped bookmark

也可以使用临时例外授权,但会导致 App Review 问题:

Is there any way to give my sandboxed Mac app read only access to files in ~/Library?

请复制我的 Apple 错误报告,请求允许此行为:

Finder Sync App Extension selectedItemURLs() should receive "User Selected File" Sandbox file access.

rdar://42874694
https://openradar.appspot.com/radar?id=5063363058991104