我已经构建了两个应用程序,即“主”应用程序和支持它的Finder扩展程序。使用 DistributedNotificationCenter ,我可以成功地在应用之间来回发布消息,并且注册的Observer事件将按预期触发。
问题似乎是我无法随事件传递任何用户数据。所有文档都建议您可以将 NSDictionary 或 [AnyHashable:Any] 对象作为 postNotificationName
的一部分传递例如:发布消息看起来像这样...
let center: DistributedNotificationCenter = DistributedNotificationCenter.default()
center.postNotificationName(NSNotification.Name(name), object: nil, userInfo: mydata, deliverImmediately: true)
这是我的Finder扩展程序发送代码:
var myInfo = [AnyHashable: Any]()
myInfo[AnyHashable("filename")] = "Test Data"
let center: DistributedNotificationCenter = DistributedNotificationCenter.default()
center.postNotificationName(NSNotification.Name("RequestSyncState"), object: nil, userInfo: myInfo, deliverImmediately: true)
并且主应用接收代码:
@objc func recievedMessage(notification:NSNotification){
NSLog ("Message Recieved from Finder Extension \(notification.name.rawValue)")
if notification.name.rawValue == "RequestSyncState" {
NSLog ("Message Recieved from Finder to determine the sync icon")
guard let userInfo = notification.userInfo else
{
return
}
guard let value = userInfo["Filename"] else
{
NSLog ("Message payload is empty")
return
}
NSLog ("Message payload is \(value)")
}
正如我所说的,这些功能会触发并收到通知,只是没有实际数据。如果我查询notification.userInfo为nil,如果我查询notification.object也为nil。
我竭尽所能,全神贯注。
答案 0 :(得分:1)
对于任何对此迷惑不解的人,原来我能使它起作用的唯一方法是发布我自己的任何数据,作为消息的一部分是将其包括在postNotificationName方法的Object属性中。跨流程边界将完全忽略UserInfo。
因此,我将自己的类CacheEntry序列化为字符串,将其发布,然后在另一侧展开。
类似这样:
func sendMessage(name:String,data:CacheEntry){
let message:String = createMessageData(messsagePayload: data)
let center: DistributedNotificationCenter = DistributedNotificationCenter.default()
center.postNotificationName(NSNotification.Name(name), object: message, userInfo: nil, deliverImmediately: true)
}
func createMessageData(messsagePayload:CacheEntry)->字符串{
let encoder = JSONEncoder()
let data = try! encoder.encode(messsagePayload)
let messsagePayloadString = String(data: data, encoding: .utf8)!
return String(messsagePayloadString)
}
func recombinantEntry(messagePayload:String)-> CacheEntry {
let jsonData = messagePayload.data(using: .utf8)!
let messsagePayloadCacheEntry = try! JSONDecoder().decode(CacheEntry.self, from: jsonData)
return messsagePayloadCacheEntry
}
@objc func recievedMessage(notification:NSNotification){
NSLog ("Message Received from Application \(notification.name)")
if notification.name.rawValue == "DSF-SetSyncState" {
NSLog ("Message Recieved from Application to set the sync icon")
let cEntry = reconstructEntry(messagePayload: notification.object as! String)
}
}
答案 1 :(得分:0)
似乎沙盒应用程序不允许使用userInfo参数
重要
已装沙箱的应用程序仅在不包含词典的情况下才能发送通知。如果发送的应用程序位于“应用程序沙箱”中,则userInfo必须为nil。