I'm writing a minimal app to learn how to read a file with UInt16 data that can be displayed as an image. It finally compiles but crashes at runtime with a file read permission error. I've attempted to give the app permission for read and write files, the permissions appear to say that I have read and write permissions.
It crashes on the second line:
let fileURL = URL(fileURLWithPath: "/Users/<user>/<some path/<some file with ext .raw>")
let data = try! Data(contentsOf: fileURL)
with this error:
Thread 1: Fatal error: 'try!' expression unexpectedly raised an error: Error Domain=NSCocoaErrorDomain Code=257 "The file “p03-4M1-041.raw” couldn’t be opened because you don’t have permission to view it." UserInfo={NSFilePath=/Users/nate/2008 11 15 Freeway Fire/p03/p03-4M1-041.raw, NSUnderlyingError=0x600002d72a90 {Error Domain=NSPOSIXErrorDomain Code=1 "Operation not permitted"}}
I think that this is an Xcode problem as the Assets.xcasssets appears to be empty and there is no plist.
I'm using Xcode 14, Swift5, on my macBook running Ventura.
A macOS app is sandboxed. To access files outside of your app's resource bundle you would need to enable the "User Selected File" file access permission in the App Sandbox section of your project's target on the "Signing and Capabilities" tab.
You would then need use the standard file chooser (such as NSOpenPanel
). Then your app would have permission to access just the selected file(s). You can't access any arbitrary file you happen to create a file URL for.
Besides enabling "User Selected File", you can also grant access to standard folders such as "Downloads" and a few others. Then your app could directly access files in the folders your app has been granted access to.