启用 AppSand box 时如何从 USB 大容量存储设备读取

How to read from usb mass storage device when AppSand box is enabled

在我的应用程序中,当用户连接 USB 设备 (phone) 时,我使用 NSWorkspaceDidMountNotification 来通知用户通过执行 NSAlert 检测到设备。

如果接受,应用会继续从设备复制(读取)文本文件内容。在我启用 AppSandbox 之前,这工作正常。 我启用了 USB 授权,但现在当应用程序尝试复制时,出现以下错误。

ERROR READING CONTENT OF FILE: Error Domain=NSCocoaErrorDomain Code=257 "The file “Office.txt” couldn’t be opened because you don’t have permission to view it." UserInfo={NSFilePath=/Volumes/OfficeDrive//documents/Office.txt, NSUnderlyingError=0x600000049390 {Error Domain=NSPOSIXErrorDomain Code=1 "Operation not permitted"}}

如果启用了 AppSandbox,是否可以从 USB 设备读取数据。

如果您在沙盒中,您只能访问用户明确授予访问权限的文件:

User-specified files. A sandboxed app (with an appropriate entitlement) automatically obtains access to files in arbitrary locations when those files are explicitly opened by the user or are dragged and dropped onto the application by the user.

(App Sandbox Design Guide)

因此您必须打开一个文件对话框(可能已经指向您从通知中找出的位置)并让用户选择它以获得访问权限。

远程相关 SO post: