Home    |    Articles    |    Talks

Another world of Notifications - Darwin Notifications

As an iOS developer, you are probably familiar with the concept of notifications and the way it’s represented by NSNotification and NSNotificationCenter in CocoaTouch. These objects are part of the Foundation framework. Because Foundation is built on top of the CoreFoundation framework, it’s not surprising that there is a CF “version” of NSNotificationCenter called CFNotificationCenter. It can be super fun to use it, because it offers you some cool additional functionality.

Darvin approved!

All NSNotifications you send are distributed just within an app’s current process. It means you won’t receive notifications send by other apps, and your notifications are not sent to other apps. This is true for NSNotificationCenter. However, there are two (three on Mac) types of CFNotificationCenters. A “local” notification center distributes notifications the same way NSNotificationCenter does, yet the “Darwin” notification center distributes notifications across the whole system! WOW

This means you can send and receive notifications between different apps, and/or an app and its extensions. See for example MMWormhole, which is using Darwin notifications for real time communication between the iPhone and Watch app.

CFNotificationCenter is a C-based API, so using it in Swift is a bit adventurous. If you like challenges (or you are Chris Eidhof), you can do it manually. However, for simple experimenting I would suggest you use CCHDarwinNotificationCenter, which wraps Darwin notifications to an object based NS API.

There is a bunch of known private system notifications distributed by the Darwin notification center, which you can listen to and make some cool stuff. See the video below and example code on GitHub. Of course, this is probably not AppStore safe.