我想從我的iPhone發送通知到手錶設備使用 CFNotificationCenterAddObserver
在手錶上和CFNotificationCenterPostNotification
。(我不是在Xcode模擬器上測試)。WatchKit達爾文通知沒有收到的手錶
這是我在iOS應用代碼:
#include <CoreFoundation/CoreFoundation.h>
...
- (void)sendLogOutNotificationToWatch{
dispatch_async(dispatch_get_main_queue(), ^{
CFNotificationCenterPostNotification(CFNotificationCenterGetDarwinNotifyCenter(), CFSTR("NOTIFICATION_TO_WATCH"), (__bridge const void *)(self), nil, TRUE);
});
}
這是我如何使用它在蘋果手錶擴展應用:
@implementation InterfaceController
.....
- (void)awakeWithContext:(id)context {
[super awakeWithContext:context];
....
[self registerToNotification];
}
- (void)registerToNotification
{
[[NSNotificationCenter defaultCenter] removeObserver:self name:@com.test.app" object:nil];
CFNotificationCenterRemoveObserver(CFNotificationCenterGetDarwinNotifyCenter(), (__bridge const void *)(self), CFSTR("NOTIFICATION_TO_WATCH"), NULL);
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(userLoggedOut) name:@"com.test.app" object:nil];
CFNotificationCenterAddObserver(CFNotificationCenterGetDarwinNotifyCenter(), (__bridge const void *)(self), didReceivedDarwinNotification, CFSTR("NOTIFICATION_TO_WATCH"), NULL, CFNotificationSuspensionBehaviorDrop);
}
void didReceivedDarwinNotification()
{
[[NSNotificationCenter defaultCenter] postNotificationName:@"com.test.app" object:nil];
}
- (void)didDeactivate {
[super didDeactivate];
[[NSNotificationCenter defaultCenter] removeObserver:self name:@"com.test.app" object:nil];
CFNotificationCenterRemoveObserver(CFNotificationCenterGetDarwinNotifyCenter(), (__bridge const void *)(self), CFSTR("NOTIFICATION_TO_WATCH"), NULL);
[[NSNotificationCenter defaultCenter] removeObserver:self];
}
- (void)userLoggedOut{
[self showAlertViewwithTitle:@"Notice" andMessage:@"User logged out on iPhone device!"];
}
您不能使用達爾文通知中心向watchos2發送消息,使用已使用WatchConnectivity框架 – dan
將消息從手錶發送到iPhone,但是我怎樣才能以其他方式使用它? –