OSX LaunchAgent LaunchDaemon通过XPC进行通信

时间:2017-11-03 06:36:53

标签: macos launchd launch-services

我正在尝试从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; 

}

在我的情况下,我收到Connection Invalidated错误,我的xpc甚至无法启动。

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

0 个答案:

没有答案