Skip to content
tezvyn:
Menu

App Groups: Sharing Data Between Your Apps & Extensions

Source: developer.apple.comMediumHow cards are made

App Groups: Sharing Data Between Your Apps & Extensions

App Groups create a shared 'lockbox' for your apps and extensions on a user's device, bypassing the default app sandbox. Use it to share UserDefaults or files between your main app and its widget. The footgun: you must use specific APIs and manage data sync.

Why it exists

By default, every iOS app lives in its own secure sandbox, isolated from all other apps for security and privacy. This prevents apps from interfering with each other but makes it impossible for related processes, like a main app and its widget, to share data directly. App Groups were created to solve this specific problem.

The mental model

Think of each app as a house with its own private property—its sandbox. An App Group is like creating a shared, secure community garden between a few specific houses that you own. Only those houses have a key to the garden's gate, allowing them to read and write data in that common ground, while all other houses on the block are kept out.

How it works

When you enable the App Group capability in Xcode for multiple targets (your app, widget, etc.), you define a unique group identifier, like 'group.com.yourcompany.yourapp'. iOS then creates a single shared directory on the file system that all members of that group can access. You can get a URL to this directory to read and write files, or you can use a special initializer for UserDefaults, UserDefaults(suiteName: groupIdentifier), to access a shared key-value store.

When to use it

Use App Groups for sharing data between a container app and its extensions, such as Widgets, Share Extensions, or Notification Service Extensions. It's also the standard way to share data between two of your own distinct apps on the same device, like a free and a pro version that need to share settings or content.

When not to use it

Do not use App Groups to communicate with apps from other developers; that's what URL schemes and other dedicated APIs are for. It is not a replacement for a backend server, as the data is only shared on that specific device. Also be aware that if the user deletes the last app belonging to the group, iOS deletes the shared container and all its data.

One canonical example

A weather app and its home screen widget need to share data. The main app fetches the forecast and saves it to a file in the shared container. The widget's timeline provider then reads that same file from the shared container to display the latest forecast. This avoids the widget making its own network request, saving battery and data. To access the shared data, the code in both targets would use the same group identifier to locate the shared directory.

Interview question

To enable a custom keyboard extension to persistently access user-defined shortcuts from its main container app, what is the most appropriate mechanism?

  • a.Passing shortcuts as parameters via a URL scheme.
  • b.Syncing shortcuts through a remote cloud database.
  • c.Using an App Group with a shared UserDefaults suite.Correct
  • d.Storing shortcuts in the main app's standard UserDefaults.
Why?

App Groups are specifically designed to allow secure, persistent data sharing between a container app and its extensions on the same device, using a shared UserDefaults suite or file system. Standard UserDefaults are sandboxed and not accessible by extensions, while URL schemes are for communication, not persistent data sharing, and cloud databases are for backend syncing, not local inter-process communication.

Just read this? Test yourself on what you have been reading.

Read the original → developer.apple.com

Put your scrolling time to good use

Learn one idea, try a quiz and save useful cards for revision. Tezvyn makes it easy to learn and stay current in your tech field, a few minutes at a time.

The iPhone app is on the way

We are building it. Until it lands, nothing here is held back from you: every interview card, your saved cards, streaks and the job board all work in Safari, plus hundreds of free practice quizzes of thirty questions each. Sign in and it all carries over to the app the day it arrives.

Want it as an icon? Tap Share at the bottom of Safari, then Add to Home Screen. It opens full screen and the cards you have read stay available offline.

Get it on Google PlayiPhone app coming soon

We are hiring for this. Open roles that interview on ios — each one lists the topics its interview covers.

See open roles