Background
First off all let's sum up what we know.
We have
- app that runs on iPhone (I will refer to it as iPhone app)
- app that runs on Watch...specifically
- UI that runs on Watch
- code that runs on iPhone as Extension.
First and last lines are most important to us. Yes, Extension is shipped to AppStore with your iPhone app, however this two things can run separately in iOS operating system. Hence, Extension and iPhone app are two different processes - two different programs that runs in OS.
Because of that fact, we can't use [NSNotificationCenter defaultCenter]
because when you try to NSLog()
defaultCenter on iPhone and defaultCenter in Extension they will have different memory address.
Darwin to the rescue!
As you might imagine, this kind of problem is not new to developers, it's proper term is Interprocess Communication. So in OS X and iOS there is...Darwin Notification mechanism. And the easiest way to use it is to implement few methods from CFNotificationCenter
class.
Example
When using CFNotificationCenter you will see it looks very similar to NSNotificationCenter. My guess is NSNotif.. was built around CFNotif.. but I did't confirmed that hypothesis. Now, to the point.
So lets assume you want to send notification from iPhone to Watch back and forth. First thing we should do is to register to notifications.
- (void)registerToNotification
{
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(didReceivedNSNotification) name:@"com.example.MyAwesomeApp" object:nil];
CFNotificationCenterAddObserver(CFNotificationCenterGetDarwinNotifyCenter(), (__bridge const void *)(self), didReceivedDarwinNotification, CFSTR("NOTIFICATION_TO_WATCH"), NULL, CFNotificationSuspensionBehaviorDrop);
}
You probably wondering why I added observer for NSNotificationCenter? In order to accomplish our task we need to create some loop, you will see it in a moment.
As for second method.
CFNotificationCenterGetDarwinNotifyCenter()
- get Darwin Notify Centre
(__bridge const void *)(self)
- notification observer
didReceivedDarwinNotification
- callBack method, fired when object receives notification.
Basically it's the same as @selector
in NSNotification
CFSTR("NOTIFICATION_TO_WATCH")
- name of the notification, same story in NSNotification, but here we need CFSTR method to convert string into CFStringRef
And finally last two parameters object
and suspensionBehaviour
- both ignored when we are using DarwinNotifyCenter.
Cool, so we registered as an observer. So lets implement our callback methods (there are two of them, one for CFNotificationCenter, and one for NSNotificationCenter).
void didReceivedDarwinNotification()
{
[[NSNotificationCenter defaultCenter] postNotificationName:@"com.example.MyAwesomeApp" object:nil];
}
Now, as you see, this method doesn't starts with - (void)Name...
. Why? Because it's C method. Do you see why we need NSNotificationCenter here? From C method we don't have access to self
. One option is to declare static pointer to yourself, like this: static id staticSelf
assign it staticSelf = self
and then use it from didReceivedDarwinNotification
: ((YourClass*)staticSelf)->_yourProperty
but I think NSNotificationCenter is better approach.
So then in selector that responds to your NSNotification:
- (void)didReceivedNSNotification
{
// you can do what you want, Obj-C method
}
When we are, finally, registered as observer we can send something from iPhone app.
For this we need only one line of code.
CFNotificationCenterPostNotification(CFNotificationCenterGetDarwinNotifyCenter(), CFSTR("NOTIFICATION_TO_WATCH"), (__bridge const void *)(self), nil, TRUE);
which can be in your ViewController, or Model.
Again, we want to get CFNotificationCenterGetDarwinNotifyCenter()
, then we specify name for notification, object that is posting notification, dictionary object (ignored when using DarwinNotifyCenter and last parameters is answer to question: deliver immediately?
In similar fashion you can send notification from Watch to iPhone. From obvious reason I suggest using different notification name, like CFSTR("NOTIFICATION_TO_IPHONE")
to avoid situation where, for example, iPhone sends notification to Watch and to itself.
To sum up
MMWormhole
is perfectly fine and well written class, even with tests that covers most, if not all, code. It's easy to use, just remember to setup your AppGroups before.
However, if you don't want to import third-party code to your project or you don't want to use it for some other reason, you can use implementation provided in this answer.
Especially if you don't want to/need to exchange data between iPhone and Watch.
There is also second good project LLBSDMessaging
. It's based on Berkeley sockets. More sophisticated and based on more low-level code. Here is link to lengthy but well written blog post, you will find link to Github there. http://ddeville.me/2015/02/interprocess-communication-on-ios-with-berkeley-sockets/.
Hope this help.