1

我创建了一个 XPC 服务。

服务器端代码简要地是

NSXPCListener *listener = [[NSXPCListener alloc] initWithMachServiceName:@"test.xpcserver"];
listener.delegate = delegate;
 [listener resume];

这是使用 info.plist 作为启动代理安装的

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
    <key>Label</key>
    <string>TestServer</string>
    <key>MachServices</key>
    <dict>
        <key>test.xpcserver</key>
        <true/>
    </dict>
    <key>ProgramArguments</key>
    <array>
        <string>/Applications/test.app/Contents/XPCServices/xpcserver.xpc/Contents/MacOS/xpcserver</string>
    </array>
    <key>RunAtLoad</key>
    <true/>
</dict>
</plist>

服务器运行良好,我可以看到它使用launchctl list运行

客户端代码在另一个应用程序中,连接代码是:

connection = [[NSXPCConnection alloc] initWithMachServiceName:@“test.xpcserver” options:0];       
  connection.remoteObjectInterface = [NSXPCInterface interfaceWithProtocol:@protocol(xpcserverprotocol)]; 
[connection resume]; 
  service = [connection remoteObjectProxyWithErrorHandler:^(NSError * _Nonnull error) { }];
   [service ServerFunc:@“howdy” withReply:^(NSString *result) {
        NSLog(@"%@",result);
    }];

但无法连接到服务器。关于出了什么问题的任何指示?

4

2 回答 2

1

从你发布的内容很难判断。检查事项:

  • 确保您的侦听器委托正在为shouldAcceptNewConnection(). 如果不正确,您将不会收到任何错误。它应该是

- (BOOL)listener:(NSXPCListener *)listener shouldAcceptNewConnection:(NSXPCConnection *)conn { your code here }

于 2020-09-08T23:14:24.940 回答
0

(为未来的读者发布这个,我想 OP 现在解决了他们的问题 :))

其他一些需要检查的事情:

  1. 确保您的启动代理/守护程序使用的是-[NSXPCListener initWithMachServiceName], 而不是+[NSXPCListener serviceListener].

    • 后者仅对常规 XPC 服务有用(它将存储在 App 的捆绑包中,而不是/Library/....

      配置错误:无法从服务包中检索 XPCService 字典。

  2. 在您的代理/守护进程中,不要忘记在[listener resume];with之后启动主运行循环[[NSRunLoop currentRunLoop] run];

  3. 在你的代理/守护进程中,确保有一个强引用来保持你的监听器delegate活着。该-[NSXPCListener delegate]属性已声明weak,因此您的侦听器可能会在您设置后立即释放。为了确定,我会在你的代表课堂上NSLog发表声明。deinit

于 2021-10-31T13:26:35.367 回答