MMWormhole is a shared message passing library that makes it easier to develop rich and interactive Today and WatchKit extensions. It’s designed to allow a containing app to share bits of data with its extensions, and to allow extensions to communicate state change back to the containing app.
The library solves a key challenge common to developing Today and WatchKit extensions. The containing app typically owns all of the data that an extension might need to drive its UI, but transferring that data to the extension is somewhat cumbersome. Even more challenging is notifying the app or extension in real time that a change has been made that needs to be reflected in its user interface. MMWormhole solves both of those problems to put the focus back on building a rich extension.
A good way to think of the wormhole is as a collection of shared mailboxes. An identifier, or address, is used to uniquely identify each mailbox you can send messages to. You know where a message will be delivered because of the identifier you associate with it, but not necessarily when the message will be picked up by the recipient. If the app is in the background, it may not receive the message immediately. It’s a good practice to check the contents of your mailbox when your app or extension wakes up, incase any messages were left there while you were away.
The key to data sharing between extensions and their containing app is the App Group feature introduced in iOS 8. MMWormhole sends messages as JSON objects that are saved as JSON files in the shared App Group. To continue the analogy above, the json file is basically the mailbox. Its address is the string identifier passed when sending a message.
MMWormhole also supports message listeners that sit on either side of the wormhole to observe changes to your messages. This is accomplished through the use of Darwin notification center accessed with CFNotificationCenterGetDarwinNotifyCenter. These notifications transcend time and space to tell the observers on the other side of the wormhole that a message was passed successfully through to the other side. This allows the extension or app to update its UI in real time, say for example if new data were obtained by the iOS app that the watch needs to present to the user.
We’re excited about the potential MMWormhole has to improve the experience of building extensions. We’ve worked on a few Today extensions at Mutual Mobile and are already hard at work on a few WatchKit extensions. This API reflects some of the lessons learned from that experience. We hope you’ll find it useful; as always, pull requests are greatly encouraged!