5

I'm trying to create an XPC service, but the service doesn't seem to being created. In my service's main method, the first thing I do is make some calls to syslog so that I can see if the service ever starts running. These log messages never appear in the log.

I've checked all the bundle identifiers, executable names, and the bundle hierarchy but still the services don't seem to be created. In the application, immediately after I create and send a message via a XPC service connection my event handler is called with an XPC_ERROR_CONNECTION_INVALID error. I'm using the C-based XPC APIs.

Is there anything else I can try and do to make sure that the XPC service at least gets created?

James Bedford
  • 28,702
  • 8
  • 57
  • 64
  • Hi, perhaps you can tell me how do I look at all the xpc services (is it done by `launchctl` or other command ? ) thanks ! – Zohar81 Dec 26 '17 at 07:59

3 Answers3

6

It turned out that I needed to code sign the application target and the XPC service target. This can be done in the Xcode project build settings, and can be set to "Ad-hoc Code Sign" without needing a Mac developer certificate.

enter image description here

James Bedford
  • 28,702
  • 8
  • 57
  • 64
2

Also check your service name. I found that the default example in the project template doesn't include the fully qualified name. In the client code, I had to change this:

NSXPCConnection *connection = [[NSXPCConnection alloc] initWithServiceName:@"MyNetworkClient"];

to this:

NSXPCConnection *connection = [[NSXPCConnection alloc] initWithServiceName:@"com.example.MyNetworkClient"];

Double check that the XPC Target's Bundle ID is what you're using for the Service Name. In my case, it wasn't launching the XPC and didn't show any errors or console logs.

Jess Bowers
  • 2,846
  • 1
  • 22
  • 42
  • Thanks! It's super-annoying that the default XPC service template doesn't get this right. – ipmcc Nov 15 '14 at 16:52
  • Hi, perhaps you can tell me how do I look at all the xpc services (is it done by launchctl or other command ? ) thanks ! – Zohar81 Dec 26 '17 at 08:09
  • Is there any way to programmatically validate the name? It works now, but I'm worried that this will silently break in future refactorings, without much of a trace to debug. – Alexander Aug 15 '18 at 05:01
  • @Alexander no i don't think you can validate. Typically these are services you control in your code base. So integration tests would be a good way to verify it works – Jess Bowers Aug 22 '18 at 18:02
  • @JessBowers Hmmm I'm having issues starting up an XPC service from my quicklook plugin target. I've validated that the service name matches the Bundle Identifier of the xpc service, and I've added a build step to copy the XPC service into `MyQuickLookplugin.qlgenerate/contents/XPCServices`. Do you know any troubleshooting steps I could take? – Alexander Aug 22 '18 at 18:04
1

I had a similar error but the solution was just: clean, clean build folder, build.

Then, by magic, it worked as intended.

Olof_t
  • 766
  • 9
  • 22