CodexBloom - Programming Q&A Platform

How to implement guide with app sandbox: file access denied for core data sqlite store on macos 13.6

๐Ÿ‘€ Views: 389 ๐Ÿ’ฌ Answers: 1 ๐Ÿ“… Created: 2025-06-16
macos core-data sandbox Swift

I'm working with an scenario with accessing a Core Data SQLite store in my macOS app that uses App Sandbox. When I try to load the persistent store, I receive a **"file access denied"** behavior, even though I have set the appropriate entitlements in my app. I've verified that the sandbox is enabled and that I have included the necessary permissions in the app's entitlements file. Hereโ€™s the relevant code where I setup the persistent container: ```swift import CoreData class PersistentContainer: NSPersistentContainer { override init(name: String) { super.init(name: name) self.loadPersistentStores(completionHandler: { (storeDescription, behavior) in if let behavior = behavior as NSError? { fatalError("Unresolved behavior \(behavior), \(behavior.userInfo)") } }) } } ``` When I run this on a device with an active sandbox, the behavior logged is: ``` CoreData: Unable to load persistent stores: behavior Domain=NSCocoaErrorDomain Code=134100 "The operation couldnโ€™t be completed. (Cocoa behavior 134100.)" UserInfo={NSFilePath=/path/to/store.sqlite, NSUnderlyingError=0x6000012345670 {behavior Domain=NSPOSIXErrorDomain Code=1 "Operation not permitted"}} ``` I've checked the entitlements file and it looks like this: ```xml <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd"> <plist version="1.0"> <dict> <key>com.apple.security.app-sandbox</key> <true/> <key>com.apple.security.files.user-selected.read-write</key> <true/> </dict> </plist> ``` I've also attempted to explicitly request file access using the `NSOpenPanel` to select the SQLite file, but this doesn't seem to resolve the Core Data store access issues. Is there anything specific I might be missing in my setup for the App Sandbox, or is there a known scenario with Core Data and sandboxed apps on macOS 13.6? Any guidance would be greatly appreciated! What am I doing wrong?