主应用和Extension之间的主动通知方法

主应用和Extension各为一个进程,常用的通信方式为被动查询,如UserDefaults的共享。如需要主动通知,则需要使用进程间的通知方式。

发通知:

CFNotificationCenterRef notification = CFNotificationCenterGetDarwinNotifyCenter ();
CFNotificationCenterPostNotification(notification, CFSTR("通知名称"), NULL,NULL, YES);
复制代码

接收通知

CFNotificationCenterRef notification = CFNotificationCenterGetDarwinNotifyCenter ();
CFNotificationCenterAddObserver(notification, (__bridge const void *)(observer), observerFunction,CFSTR("通知名称"), NULL, CFNotificationSuspensionBehaviorDeliverImmediately);
复制代码

其中observerFunction为通知响应函数名,为C语言格式,参数格式如下

void observerFunction (CFNotificationCenterRef center, void *observer, CFStringRef name, const void *object, CFDictionaryRef userInfo){
    NSLog(@"--------------函数响应--------");
}
复制代码
© 版权声明
THE END
喜欢就支持一下吧
点赞0 分享