acw
acw

Reputation: 1093

Shared NSUserDefault between sandboxed OSX app and extension

Background

I'm developing an OSX application with an associated Today Widget extension. I'd like for the main app and the extension to share user preferences, much like is described in Sharing Data with Your Containing App from Apple. In my case, the main app writes the preferences and the extension reads them.

Both the app and the extension are sandboxed. They also are members of the same application group. Relevant entitlements, verified using codesign -d --entitlements, on both the app and extension:

com.apple.security.app-sandbox: true
com.apple.security.application-groups:
  - com.example.Common

Code

The app and extensions share the same code to initialize and access NSUserDefaults.

NSUserDefaults.init(suiteName: "com.example.Common")

Problem

The main app creates the preferences in:

~/Library/Containers/com.example/Data/Library/Preferences/com.example.Common.plist

When the extension is then opened, its NSUserDefault instance is empty.

If I move the plist created by the app to:

~/Library/Group Containers/com.example.Common/Library/Preferences/com.example.Common.plist

The extension's NSUserDefault instance is populated with the expected values.

Note: The following doesn't happen when building and running the app from Xcode, only when selecting Product -> Archive, then Export -> Export as a Mac Application. When building/running from Xcode, the main app creates the plist file where the extension expects it as described above.

Upvotes: 6

Views: 876

Answers (1)

rndev
rndev

Reputation: 792

I do same thing right now — sandbox app + Share Extension + NSUserDefault + application groups, and it's work fine. Maybe Apple fixed few bugs during this time?

Upvotes: 0

Related Questions