Общайтесь с другим приложением с помощью XPC
У меня есть оконное приложение, и для добавления некоторых функций мне нужно другое приложение, которое запускается при входе в систему и синхронизирует данные с сервером, если доступно.
Я пытался с NSDistributionNotification, но это практически бесполезно в изолированном приложении. Я посмотрел XPC и надеялся, что он будет работать, но я просто не знаю, как заставить его работать с помощником. До сих пор я сделал это с помощью XPC.
Основное приложение
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);
}];
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);
}
Это работает нормально, теперь мне нужно передать этот результат в приложение Helper. Как я могу это сделать ? Если XPC не является ответом здесь, чтобы общаться, то какой я должен использовать? Любые указатели, пожалуйста?