Apple Watch : Sending notifications to the watch from the iPhone

The normal way to communicate between the AppleWatch and the iPhone is via AppGroups to share data (shared Prefs, CoreData etc) or by calling the method openParentApplication from the watch and using the delegate method handleWatchKitExtensionRequest.

What would be very nice is if you could notify the watch from the iPhone that something happened, something like a postNotification.  Sadly postNotificaitons do not work, but Darwin ones do!

WatchKit code

– (void)registerTestNotification {

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(MY_METHOD) name:@”com.burfdevelopment.test” object:nil];

CFNotificationCenterAddObserver(CFNotificationCenterGetDarwinNotifyCenter(), (__bridge const void *)(self), didReceivedDarwinNotification, CFSTR(“UPDATE_WATCH”), NULL, CFNotificationSuspensionBehaviorDrop);

}

static void didReceivedDarwinNotification() {

[[NSNotificationCenter defaultCenter] postNotificationName:@”com.burfdevelopment.test” object:nil];

}

iPhone code to fire off notification

CFNotificationCenterPostNotification(CFNotificationCenterGetDarwinNotifyCenter(), CFSTR(UPDATE_WATCH), (__bridge const void *)(self), nil, TRUE);

Leave a Reply