I was developing a Safari App Extension and needed a way to share data between the containing app and the extension. I needed to pass the user settings, so the extension would behave correctly.
The containing app does not have a direct access to the extension’s properties and vice versa. The sharing can happen through
First I navigated to the containing app’s Project Navigator and selected the main app target. Under the Capabilities tab I toggled on the App Groups.
Then I created a new app group: “group.com.bundle-identifier-domain.myapp”.
I added the same app group for app extension target.
Now I could finally share the user settings between the containing app and the extension by initializing the userDefaults with the group:
let userDefaults = UserDefaults.init(suiteName: "group.com.bundle-identifier-domain.myapp")
Note: The app group can be created also in the Apple’s Developer Portal under the “Certificates, Identifiers & Profiles” sections.