DistributedNotificationCenter-如何在应用程序之间传递数据? [英] DistributedNotificationCenter - How to pass data between applications?

查看:65
本文介绍了DistributedNotificationCenter-如何在应用程序之间传递数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经构建了两个应用程序,即主"应用程序和支持该应用程序的Finder扩展程序.使用 DistributedNotificationCenter ,我可以成功地在应用程序之间来回发布消息,并且注册的Observer事件将按预期触发.

I have built two apps the "Main" app and a Finder Extension which supports it. Using DistributedNotificationCenter I can successfully post messages back and forth between apps and the registered Observer event fires as expected.

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

The issue seems to be that I cannot pass any user data with the event. All documentation suggests you can pass a NSDictionary or [AnyHashable: Any] object as part of the postNotificationName

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

Eg:Posting the message looks something like this...

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

这是我的Finder扩展程序发送代码:

Here is my Finder Extension sending code:

    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.

As I said these functions fire and the notification is received, just no actual data. If I query notification.userInfo it is nil and if I query notification.object it too is nil.

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

I tried everything I can think of and I am at a total loss.

推荐答案

对于任何对此迷迷糊糊的人,事实证明,我能使之起作用的唯一方法是发布我自己的任何数据,作为消息的一部分将其包括在postNotificationName方法的Object属性中.跨流程边界将完全忽略UserInfo.

For anyone who stumbles on this, it turns out the only way I could make it work was to post any of my own data as part of the message was to include it in the Object property on the postNotificationName method. UserInfo is completely ignored across the process boundaries.

因此,我将自己的类CacheEntry序列化为字符串,将其发布,然后在另一侧展开.

So I serialised my own classs CacheEntry into a string, post it, then unwrap it on the other side.

所以像这样:

func sendMessage(name:String,data: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)->字符串{

func createMessageData(messsagePayload:CacheEntry) -> String {

    let encoder = JSONEncoder()
    let data = try! encoder.encode(messsagePayload)
    
    let messsagePayloadString = String(data: data, encoding: .utf8)!

    return String(messsagePayloadString)
}

func recombinantEntry(messagePayload:String)->CacheEntry {

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)

    }
    

    }

这篇关于DistributedNotificationCenter-如何在应用程序之间传递数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆