I am working on an application where i have separated out two different XPC services from the main application. I want a XPC service to communicate with other XPC service which will do some processing and will return the data back to first service and that first service will do its own processing and then will give data back to the main application. I even tried this but communicating between the services give error that "could not communicate with helper application".
My question is that either this is possible or not? If yes that what is required?
Any help would be appreciated.
Yes, this is possible, but not at all obvious. I asked questions about this exact thing on and off for a year before an obscure hint from an Apple engineer led me to stumble across the answer.
The trick is that you need to transfer the
NSXPCListenerEndpoint
of one process to another process. That second process can then use that endpoint object to create a direct connection with the first process. The catch is that, whileNSXPCListenerEndpoint
isNSCoding
compliant, it can only be encoded through an existing XPC connection, which makes this problem sound like a catch-22 (you can't transfer the endpoint until you've created a connection, and you can't create a connection until you have the endpoint).The solution ("trick") is you need an intermediating process (let's call it "cornerstone") that already has an XPC connections that can exchange endpoints between the other two processes.
In my application I ended up creating a daemon process which acts as my cornerstone, but I think you could do it directly in your application. Here's what you need to do:
listener = NSXPCListener.serviceListener
) or create a dedicated, anonymous, listener for the second process (usinglistener = NSXPCListener.anonymousListener
).listener.endpoint
)[[NSXPCConnection alloc] initWithListenerEndpoint:aEndpoint]]
.