gpt4 book ai didi

swift - DistributedNotificationCenter - 如何在应用程序之间传递数据?

转载 作者:行者123 更新时间:2023-12-05 02:54:04 28 4
gpt4 key购买 nike

我已经构建了两个应用程序“主”应用程序和一个支持它的 Finder Extension。使用 DistributedNotificationCenter 我可以成功地在应用程序之间来回发布消息,并且已注册的 Observer 事件按预期触发。

问题似乎是我无法通过事件传递任何用户数据。所有文档都建议您可以传递 NSDictionary[AnyHashable: Any] 对象作为 postNotificationName

的一部分

例如:发布消息看起来像这样......

let center: DistributedNotificationCenter = DistributedNotificationCenter.default()
center.postNotificationName(NSNotification.Name(name), object: nil, userInfo: mydata, deliverImmediately: true)

这是我的 Finder Extension 发送代码:

    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。

我尝试了所有我能想到的方法,但我完全不知所措。

最佳答案

对于偶然发现此问题的任何人,事实证明,我唯一能让它起作用的方法是发布我自己的任何数据作为消息的一部分,将其包含在 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 reconstructEntry(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)

}


}

关于swift - DistributedNotificationCenter - 如何在应用程序之间传递数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61963965/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com