1

My helper agent app needs to constantly perform one activity after its launch, but it also needs to communicate via XPC, to the main app

However, the setup of the listener requires a specific main function, and that takes over the app (omitting the standard AppDelegate NSApplication approach)

int main(int argc, const char *argv[])
{
    NSString *bundleId = [[NSBundle mainBundle] bundleIdentifier];
    NSXPCListener *listener = [[NSXPCListener alloc] initWithMachServiceName:bundleId];
    XX *xx = [XX new];
    listener.delegate = xx;
    [listener resume];
    return 0;
}

How to have both? Background app with NSRunLoop and NSXPCListener app?

Peter Lapisu
  • 19,915
  • 16
  • 123
  • 179

1 Answers1

0

NSApplication is part of the AppKit framework. So if you write a command line tool as agent, you can't use this. But you can of course still us NSRunLoop. Simply start a runloop with [[NSRunLoop currentRunLoop] run];. Adding this after your [listener resume] should keep the process up and running.

  • but when i add [[NSRunLoop currentRunLoop] run]; after [listener resume]; it never gets called, as the resume takes over the app :/ – Peter Lapisu Aug 12 '14 at 17:29
  • If the listener would lock up your program, it would never return and if I understand your question correctly, that's the actual problem. I just ran the code and it works fine for me. Program keeps running and I can connect through XPC. If I see correctly, your helper is outside the app bundle? Because you're exposing a Mach service. Are you trying to use an XPC bundle outside of your app bundle? – Marian Julius Zange Aug 13 '14 at 22:11
  • the helper should independently periodically perform task and also communicate by XPC... – Peter Lapisu Aug 14 '14 at 10:01