Swift file reading permission error on macOS sandbox

徘徊边缘 提交于 2020-01-06 04:36:43

问题


I'm trying to read the contents of a file on the filesystem in a macOS Swift app (Xcode 9 / Swift 4).

I'm using the following snippet for it:

let path = "/my/path/string.txt"
let s = try! String(contentsOfFile: path)
print(s)

My problem is the following:

  1. This works in a Playground
  2. This works when I use the Command Line Tool macOS app template
  3. This terminates in a permission error when I use the Cocoa App macOS app template

The permission error is the following:

Fatal error: 'try!' expression unexpectedly raised an error:

Error Domain=NSCocoaErrorDomain Code=257 "The file "data.txt" couldn't be opened because you don't have permission to view it."

UserInfo={NSFilePath=/my/path/data.txt, NSUnderlyingError=0x60c0000449b0 {Error Domain=NSPOSIXErrorDomain Code=1 "Operation not permitted"}}

I guess it's related to sandboxing but I found no information about it.

  1. How can I read from the filesystem in a sandboxed app? I mean there are so many GUI apps which need an Open File dialog, it cannot be a realistic restriction of sandboxed apps to not read files from outside the sandbox.

  2. Alternatively, how can I switch off sandboxing in Build Settings?

  3. Finally, I tried to compare the project.pbxproj files between the default Cocoa Apps and Command Line Tool template and I didn't see any meaningful difference, like something about security or sandbox. If not here, where are those settings stored?


回答1:


Your bullet 1: you have the answer, you need to use NSOpenPanel to obtain the user's permission to access the file. You can use NSOpenPanel methods to restrict the user's choice to the cancel button and just the file you wish - thus making a "request access to" dialog. Start with Apple's App Sandbox Design Guide.

Your bullet 2: you've the right idea, just the wrong tab, you should find the sandboxing switch on the Summary tab.

HTH



来源:https://stackoverflow.com/questions/48493813/swift-file-reading-permission-error-on-macos-sandbox

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!