gpt4 book ai didi

objective-c - XPC服务支持多种协议(protocol)

转载 作者:行者123 更新时间:2023-12-05 07:13:33 25 4
gpt4 key购买 nike

我的 Xcode 项目中有一个生成 XPCService 的目标。现在我想实现更多不同上下文的功能,所以我想将它们添加到不同的协议(protocol)中。

我希望当前的 xpc 服务支持来自这两种协议(protocol)的连接。

单一协议(protocol)支持的默认代码如下:

    // Create the delegate for the service.
ServiceDelegate *delegate = [ServiceDelegate new];

// Set up the one NSXPCListener for this service. It will handle all incoming connections.
NSXPCListener *listener = [NSXPCListener serviceListener];
listener.delegate = delegate;

// Resuming the serviceListener starts this service. This method does not return.
[listener resume];

ServiceDelegate 有以下方法:

- (BOOL)listener:(NSXPCListener *)listener shouldAcceptNewConnection:(NSXPCConnection *)newConnection {

我果断地为该连接设置了协议(protocol),没有选择 myFirstProtocol

的选项

newConnection.exportedInterface = [NSXPCInterface interfaceWithProtocol:@protocol(myFirstProtocol)];

现在我也有 mySecondProtocol,我想根据我在客户端发送的连接属性来选择协议(protocol)。我正在寻找某种标识符来帮助我选择正确的界面。

谢谢!

最佳答案

我正在尝试动态设置接口(interface)。

所以,像这样:1.让服务导出一个只有一个方法的协议(protocol)requestInterface(kind: String)2. 让连接进程调用requestInterface,并用一个字符串指定它想要什么样的接口(interface)3. 此时更改 exportedInterface

关于objective-c - XPC服务支持多种协议(protocol),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60150098/

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