Comunícate con otra aplicación usando XPC
Tengo una aplicación con ventana, y para agregar algunas funcionalidades necesito otra aplicación que se inicie al iniciar sesión y sincronice los datos con el servidor si está disponible.
He intentado con NSDistributionNotification pero es prácticamente inútil en una aplicación de espacio aislado. Busqué XPC y esperaba que funcionara, pero no sé cómo hacerlo funcionar con el ayudante. Hasta ahora he hecho esto usando XPC.
Aplicación principal
NSXPCInterface *remoteInterface = [NSXPCInterface interfaceWithProtocol:@protocol(AddProtocol)];
NSXPCConnection *xpcConnection = [[NSXPCConnection alloc] initWithServiceName:@"com.example.SampleService"];
xpcConnection.remoteObjectInterface = remoteInterface;
xpcConnection.interruptionHandler = ^{
NSLog(@"Connection Terminated");
};
xpcConnection.invalidationHandler = ^{
NSLog(@"Connection Invalidated");
};
[xpcConnection resume];
NSInteger num1 = [_number1Input.stringValue integerValue];
NSInteger num2 = [_number2Input.stringValue integerValue];
[xpcConnection.remoteObjectProxy add:num1 to:num2 reply:^(NSInteger result) {
NSLog(@"Result of %d + %d = %d", (int) num1, (int) num2, (int) result);
}];
Servicio XPC
In main () ...
SampleListener *delegate = [[SampleListener alloc] init];
NSXPCListener *listener = [NSXPCListener serviceListener];
listener.delegate = delegate;
[listener resume];
// In delegate
-(BOOL)listener:(NSXPCListener *)listener shouldAcceptNewConnection:(NSXPCConnection *)newConnection {
NSXPCInterface *interface = [NSXPCInterface interfaceWithProtocol:@protocol(AddProtocol)];
newConnection.exportedInterface = interface;
newConnection.exportedObject = [[SampleObject alloc] init];
[newConnection resume];
return YES;
}
// In Exported Object class
-(void)add:(NSInteger)num1 to:(NSInteger)num2 reply:(void (^)(NSInteger))respondBack {
resultOfAddition = num1 + num2;
respondBack(resultOfAddition);
}
Esto funciona bien, ahora necesito pasar este resultado a la aplicación Helper. Cómo puedo hacer esto ? Si XPC no es la respuesta aquí para comunicarse, ¿cuál debo usar? ¿Alguna sugerencia por favor?