Sharing data with App Groups

When you're developing an application or extension that's part of a suite of apps, extensions, or a combination of both, you're probably going to want to share some code and data. You already saw how to share code by including a file in multiple targets. This doesn't immediately allow for data sharing, though.

To share data between apps you make use of App Groups. An App Group is a group of applications that have the App Groups entitlement enabled in their capabilities. More importantly, the applications in an App Group must specify that they want to share information with a certain group.

An App Group is specified as a unique string. Every app or extension that is part of an App Group must have this same unique string added to the list of groups the target belongs to. A single target can be part of several App  Groups. To enable App  Groups, select your target and go to its capabilities tab. Next, enable the App Groups capability and specify a unique identifier for your group.

Let's do this for the The Daily Quote and its Today Extension right away. Refer to the following screenshot to make sure that you've enabled the capability correctly. Don't forget to repeat the steps for both the app and the extension. Every target that should be part of an App Group should specify this separately:

Sharing data with App Groups

Now that the App Group is enabled, we have gained several capabilities. First and foremost, we've gained the ability to share UserDefaults between our app and our extension. You'll see how to do this soon.

Another powerful feature we've gained is the ability to read and write data to a shared folder. This means that we could share an entire CoreData database between multiple targets through App  Groups. Doing this comes with a couple of risks that we won't go into too much. The most important takeaway is that you should always make sure that you use appropriate locking mechanisms if you're reading or writing data from a shared container. Even though it's unlikely, it's possible that two apps read or write data from the same file at the same time. This could cause data corruption and Apple has made it the responsibility of the developers themselves to ensure that you perform your read and write operations safely and responsibly.

Let's get back to The Daily Quote. This app uses UserDefaults to store the date on which the most recent quote was created and it's also responsible for picking and storing a new quote. In the current setup, the app and the widget both use their own UserDefaults instance. We just saw that it's possible to improve on this with App Groups. Doing this is probably a lot simpler that you would expect. This line of code is what we currently use to obtain an instance of UserDefaults:

private static var userDefaults = UserDefaults.standard 

To use our shared UserDefaults instead of the standard one we only need to update that single line of code by creating our own instance of UserDefaults and initializing it with a suiteName parameter:

private static var userDefaults = UserDefaults(suiteName: "group.donnywals.quotetoday")! 

Because the initializer for UserDefaults can fail, it returns an optional instance. This would happen if we were to try to initialize the UserDefaults with a group that we don't have access to. In this example, we're pretty sure that we have set everything up correctly so the instance is for unwrapped. Note that this is not always the best idea, and you might want to stick with the optional instance in your application instead.

Running the app and widget now will result in your widget and app always using the exact same quote. The reason they use the same quote is that they now use a shared UserDefaults store, and because they already share the Quote struct, we only had to make sure that this struct uses the shared store instead of the target-specific store.

Now that our app and widget share their UserDefaults, we have successfully implemented out Today Extension and widget.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.135.207.129