XPC服务支持多种协议

时间:2020-02-10 12:06:09

标签: objective-c swift macos xpc

我在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,并且我想根据我在客户端发送的连接属性选择协议。.我正在寻找某种标识符,以帮助我选择正确的接口。

谢谢!

1 个答案:

答案 0 :(得分:0)

我正在尝试动态设置界面。

因此,类似: 1.让服务导出具有单一方法requestInterface(kind: String)的协议 2.让连接过程使用字符串指定其需要哪种接口来调用requestInterface 3.那时更改exportedInterface