Sharing data between App and Extension on iOS

emre gürses
3 min readJan 1, 2023

Using extension is very populer in iOS Aps. But you can not access directly your data(userdefaults) from in your extension. With this article, I will explain on my shared app groups experience to share data between app and extensions.

If you want to add some futures to your app such as Sign in Apple, ApplePay,NFC, Siri, HealtyKit, HomeKit, Cloud, etc you should use Extension. In some cases, you ought to need to share data between your app and extension. If so, you can choose one of them which is explain below to share data. I will describe steps of using shared app groups.

There are several ways to share data between an app and its extensions on iOS. Some common methods include using shared app groups, using the Pasteboard, and using shared keychain access.

  1. Shared app groups allow multiple apps and extensions to access a shared container for storing data. To use shared app groups, the app and its extensions must be associated with the same app group in the developer portal, and they must use the same app group identifier when accessing the shared container.
  2. The Pasteboard is a system-wide clipboard that allows apps and extensions to share data. To use the Pasteboard, apps and extensions can write data to the Pasteboard and read data from the Pasteboard using the appropriate APIs.
  3. Shared keychain access allows multiple apps and extensions to access a shared keychain for storing sensitive data, such as passwords and encryption keys. To use shared keychain access, the app and its extensions must be associated with the same keychain access group in the developer portal, and they must use the same keychain access group identifier when accessing the shared keychain.

It is important to note that data sharing between an app and its extensions is subject to the same app sandboxing rules as any other data sharing on iOS. This means that apps and extensions can only share data if they have the appropriate entitlements and permissions.

Here is a code snippet that demonstrates how to use App Groups to share data between an app and an extension in iOS:

First, you will need to create an App Group in your developer account and enable it for your app and extension. You can enable App Groups in your project like screenshot.

Signing&Capabilities -> + Capability -> App Groups

Then, in your app and extension, you can use the following code to store and retrieve data from the shared App Group container:

Xcode-> Signing&Capabilities->AppGroup enabled

We can choose any App Group name as a reverse domain that should start with group.com. For instance, we choose group.com.trynew.sharedata as a group name and we want to save ID. Let’s look at the below code snipped.

let defaults = UserDefaults(suiteName: "group.com.trynew.sharedata")
defaults?.set("555644", forKey: "userID")
defaults?.synchronize()

After saving data in app, you can retrieve this data from extension. We can use below code to get data.

let defaults = UserDefaults(suiteName: "group.com.trynew.sharedata")
let greeting = defaults?.string(forKey: "userID")

Replace “group.com.trynew.sharedata” with the actual App Group ID that you have configured in your developer account. The UserDefaults class is used to store and retrieve data from the shared container, and the synchronize method is used to ensure that the data is persisted.

It’s important to note that App Groups are only accessible to apps and extensions that are signed with the same App ID prefix and are added to the same App Group.

I will share source code in github as soon as possible.

--

--

emre gürses

Denizbank — Intertech, Mobil Uygulama Geliştiricisi(iOS)