【问题标题】:XPC service to support multiple protocolsXPC服务支持多种协议
【发布时间】:2020-02-10 12:06:09
【问题描述】:

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

我希望当前的 xpc 服务支持两种协议的连接。

单协议支持的默认代码如下所示:

    // 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 {

我果断地设置了该连接的协议,没有选择myFirstProtocol

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

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

谢谢!

【问题讨论】:

  • 您是否有多个连接(以及导出的对象)?

标签: objective-c swift macos xpc


【解决方案1】:

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2023-03-29
    • 2021-05-11
    • 2014-09-04
    • 1970-01-01
    • 1970-01-01
    • 2018-07-01
    • 1970-01-01
    • 2018-09-20
    相关资源
    最近更新 更多