gpt4 book ai didi

swift - 无法让 XPC 从客户端接收数据

转载 作者:行者123 更新时间:2023-11-28 07:21:36 31 4
gpt4 key购买 nike

我正在尝试获得一个简单的命令行 Swift 应用程序,稍后将用于与 Chrome\Firefox 进行本地消息传递,以与工作场所的其他应用程序进行通信。

主 UI 应用程序(即 XPC 服务器):

//
// AppDelegate.swift
//

import Cocoa
import IOKit.ps
import Foundation
import Dispatch

@NSApplicationMain
class AppDelegate: NSObject, NSApplicationDelegate {

func applicationDidFinishLaunching(_ aNotification: Notification) {
let queue = DispatchQueue(label: "com.my.label");
let conn:xpc_connection_t = xpc_connection_create_mach_service("com.test.xpc", queue, UInt64(XPC_CONNECTION_MACH_SERVICE_LISTENER));
DispatchQueue.main.async {
xpc_connection_set_event_handler(conn) { [weak self] client in
debugPrint("client", client);
xpc_connection_set_event_handler(client) { [weak self] object in
debugPrint("server received message:", xpc_copy_description( object ));
}
xpc_connection_resume(client);
}
print("inside dispatch");
xpc_connection_resume(conn);
}
debugPrint("here after listener")
}

func applicationWillTerminate(_ aNotification: Notification) {
// Insert code here to tear down your application
}

}

命令行应用程序(即 XPC 客户端)

//
// main.swift
//

import Foundation
import XPC

let queue = DispatchQueue(label: "com.my.queue");
let conn:xpc_connection_t = xpc_connection_create_mach_service("com.test.xpc", queue, 0);

DispatchQueue.main.async {
xpc_connection_resume(conn);
}

var message:xpc_object_t = xpc_dictionary_create(nil, nil, 0 );
xpc_dictionary_set_string( message, "message", "hello world" );

xpc_connection_send_message(conn, message);

debugPrint("Exiting")

当我运行命令行应用程序时,服务器没有收到任何消息,所以我猜测要么是客户端应用程序没有找到它,要么是服务器没有正确设置。

有什么建议吗?

谢谢

最佳答案

也许是这样的:

用户界面:

let queue = DispatchQueue(label: "com.my.label");

命令行界面:

let queue = DispatchQueue(label: "com.my.queue");

你们不应该使用同一个队列吗?

关于swift - 无法让 XPC 从客户端接收数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57852732/

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