我已经创建了一个 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);
}];
但无法连接到服务器。 关于出了什么问题的任何指示?
最佳答案
很难从您发布的内容中判断出来。要检查的事项:
- 确保您的监听器委托(delegate)为
shouldAcceptNewConnection()
实现正确的函数签名。如果不正确,您将不会收到任何错误。应该是
- (BOOL)listener:(NSXPCListener *)listener shouldAcceptNewConnection:(NSXPCConnection *)conn { 你的代码在这里 }
- 使用系统日志记录 (NSLog) 或为您的服务添加 stdout 和 stderr 日志记录。请参阅此 hello_world_xpc example 中的
launch.sh
,它根据本地路径生成 plist。该示例使用的是 C API,因此不完全是您要查找的内容。 - 在此处查看旧的 Objective-C hello.tar.gz 示例:afewguyscoding.com/2012/07/ipc-easy-introducing-xpc-nsxpcconnection/
关于macos - 无法连接到 Launch 代理中的 XPCService,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62895588/