WFDistributedNotificationCenter
是用于在您的应用和扩展之间进行通信的通知中心。
NSNotificationCenter
的API兼容userInfo
字典的通知WFDistributedNotificationCenter *center = [[WFDistributedNotificationCenter alloc] initWithSecurityApplicationGroupIdentifier:@"group.test"];
[center postNotificationName:@"UpdatedStuff" object:nil userInfo:@{@"ChangedIds": @[@3,@4,@10]}];
将WFDistributedNotificationCenter.h
和WFDistributedNotificationCenter.m
添加到您的项目中。
分布式通知的传统模式是客户端-服务器,其中应用是客户端,一个守护程序(如distnoted
、notifyd
等)作为服务器。服务器从其客户端接收通知并相应地分发
这种模式在iOS上不可行。没有支持发布丰富通知的守护程序,您也不能创建自己的。在WFDistributedNotificationCenter
中,连接是去中心化的。当应用想要监听通知时,它们创建一个本地的Mach服务器并更新一个共享注册表,其中包含Mach端口名称。发布通知的客户端读取此注册表并将通知发送到适当的应用进程。这种通信是通过公共的CFMessagePort
API进行的,共享注册表目前存储在应用组容器中。
userInfo
字典中发送任何遵循NSSecureCoding
的对象,前提是观察者指定了allowedClasses
中的类WFNotificationCenter可在MIT许可证下获得。有关更多信息,请参阅LICENSE文件。