2

我正在尝试从 LaunchDaemon 传达我的 xpc 服务。

LaunchDaemon 是命令行可执行文件,LaunchAgent 是 xpc 服务。

从主服务(LaunchDaemon)启动我的连接,如下所示:

NSXPCConnection * _connectionToService;

 _connectionToService = [[NSXPCConnection alloc] initWithMachServiceName:@"com.XpcService" options:NSXPCConnectionPrivileged];


 _connectionToService.remoteObjectInterface = [NSXPCInterface interfaceWithProtocol:@protocol(XpcServiceProtocol)];

 _connectionToService.interruptionHandler = ^{ NSLog(@"Connection Terminated"); };

 _connectionToService.invalidationHandler = ^{ NSLog(@"Connection Invalidated"); };

 [_connectionToService resume];

 //here calling required functions

Xpc-service 监听如下:

@interface ServiceDelegate : NSObject <NSXPCListenerDelegate>
@end 

@implementation ServiceDelegate

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

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

XpcService *exportedObject = [XpcService new];

newConnection.exportedObject = exportedObject;

[newConnection resume]; 

return YES; 

} 

@end 

int main(int argc, const char *argv[]) { 

ServiceDelegate *delegate = [ServiceDelegate new];

NSXPCListener *listener = [[NSXPCListener alloc] initWithMachServiceName:@"com.XpcService"]; 

listener.delegate = delegate;

[listener resume]; 

return 0; 

}

在我的情况下,我收到连接无效错误,我的 xpc 甚至无法启动。

LaunchAgent & LaunchDaemon 加载完美,代码签名也完成。帮我找出可能导致问题的原因?提前致谢。

4

0 回答 0