
基础产品
适用各种通信场景,接入灵活

融合场景
专为场景打造,接入成本低

扩展能力
核心能力延展,功能全面

更多服务
一站式服务,满足各种需求
回答:
我们可以使用系统级的通知中心 CFNotificationCenter 来实现拓展间通知通信,使用时和 NSNotificationCenter 类似,注意监听和移除。
示例代码:
1、 宿主 App 需要注册两个监听,CFNotificationCenter 用来接收拓展通知,NSNotificationCenter 类型的用来在宿主收到拓展通知后,在宿主 App 中进一步执行通知方法:
//注册宿主 App 处理事件的监听 [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(receiveExtensionMessageNotification:) name:@"testNotification" object:self]; //注册 CFNotificationCenter 监听 - (void)registerForNotificationsWithIdentifier{ [self unregisterForNotificationsWithIdentifier]; CFNotificationCenterRef const center = CFNotificationCenterGetDarwinNotifyCenter(); CFNotificationCenterAddObserver(center, (__bridge const void *)(self),observerCFNotification, CFSTR("ScreenShare Start"), NULL, CFNotificationSuspensionBehaviorDeliverImmediately); } void observerCFNotification(CFNotificationCenterRef center, void *observer, CFStringRef name, const void *object, CFDictionaryRef userInfo) { // 处理 Extension 的通知 //此处应该发送 NSNotificationCenter 的通知,然后在该通知中做自己的操作 [[NSNotificationCenter defaultCenter] postNotificationName:@"testNotification" object:(__bridge NSObject *)(observer) userInfo:@{@"identifier":(__bridge NSString *)name}]; } //移除监听 - (void)unregisterForNotificationsWithIdentifier { CFNotificationCenterRef noti = CFNotificationCenterGetDarwinNotifyCenter (); CFNotificationCenterRemoveObserver(noti, (__bridge const void *)(self), CFSTR("ScreenShare Start"), NULL); }
//拓展中,在想要发送通知的地方实现:
//发送通知 CFNotificationCenterRef noti = CFNotificationCenterGetDarwinNotifyCenter(); CFNotificationCenterPostNotification(noti, CFSTR("ScreenShare Start"), NULL,NULL, YES);