When Apple first created the iPhone, they had a lot to think about with regards to security. The real issues arose with the release of the iOS SDK, which opened the floodgates to developers ready to create their own apps.To ensure no malicious activity leaked out from an app into the core operating system, Apple designed iOS with what metaphorically resembles reinforced concrete walls surrounding each app. Apps are completely isolated and unaware of each other, creating a secure ecosystem that takes user data protection very seriously.
In iOS 8, Apple introduced App Extensions, a new method of interacting with an app without launching it. Though the secure architecture of iOS prevents apps from hijacking each other, it causes a headache when passing data between an app and its extension.
Types of App Extensions
App extensions are not standalone apps; they provide extended functionality of the app that is meant to be efficient and focused towards a single task. Here are the current types of extensions:
- Today - An extension displayed in the Today view of the Notification Center which shows brief information and allows performance of quick tasks
- Share - An extension that enables your app to share content with users on social networks and other sharing services
- Action - An extension which allows the creation of custom action buttons in the Action Sheet to let users view or transform content originating in a host app
- Photo Editing - An extension that lets users edit a photo or a video within the Photos app
- Document Provider - An extension used for allowing other apps to access the documents managed by your app
- Custom Keyboard - An extension that replaces the system keyboard
Sharing Data: NSUserDefaults and App Groups
Despite being delivered to the user’s device through the App Store as part of the containing app’s binary, the extensions run in a separate process than the main app. This means the extension and the app have different sandboxes. In other words, there is no communication between them. This goes back to the security walls that Apple built into iOS.
So if there is no communication between an app and it’s extension, how is data passed between them? In Apple’s Lister demo app uses iCloud with Core Data to sync data between an app and its extension. This has the benefit of system-level caching to avoid network delays, however, Core Data with iCloud is not easy to setup and comes with its own set of issues. The easiest way to share data is through a new concept called App Groups, which are allowed to share some data using NSUserDefaults. With the NSUserDefaults class you can save settings and properties related to app or user data. For example, you could save a profile image set by the user or a default color scheme for the app.
Here’s how to use NSUserDefaults with App Groups to pass data between your main app and your extension:
- In your main app, select your project in the Project Navigator.
- Select your main app target and choose the Capabilities tab.
- Toggle the App Groups switch to ON. This will communicate with the Developer Portal in order to generate a set of entitlements.
- Create a new container. According to Apple, your container ID must start with “group”, so a name such as “group.io.intrepid.myapp” is perfect.
- Select your extension target and repeat the process of enabling App Groups. Do not create a new App Group, simply select the group that was just created in the main app target.
- When reading or writing NSUserDefaults in either your app or your extension, do not access NSUserDefaults.standardUserDefaults(). Instead use NSUserDefaults(suiteName: "group.io.intrepid.myapp"). Note: The suite name is the name of your App Group container created in Step 4.
Here is an example of saving to NSUserDefaults with your App Group:
Here is an example of reading from NSUserDefaults with your App Group:
Now, both your extension and your app will be accessing the same set of NSUserDefaults, so you can freely pass data between them. In the code examples above, you just set the specific color scheme that your keyboard might use. If the user picked the blue themed keyboard in your main app and then opened your custom keyboard, you will be able to access their chosen theme.
App extensions are an incredible feature that any app can take advantage of. No matter what type of extension you decide to create, they all provide another entrance into your core app. But remember, don’t go creating a game in a keyboard extension. Rather, add a Today extension to show your high score leaderboard! These extensions should not and are not meant to replace the functionality of your main app– they are merely designed to provide additional resources.