1

My platform contains system wide Daemon based on mixed c++/objective-c code which operated by launchd according the plist file with the proper configuration reside in /Library/LaunchDaemons/.

On the other side, it contains UI based application built using storyboard which runs from the background and defined as LaunchAgent (meaning that it's also operated by launchd using plist file in /Library/LaunchAgents/)

Now I wish to establish a uni directional connection between them so that the Daemon can send messages to be displayed by the UI application. the messages can contain strings/numbers or any other displayable data.

I've checked the option using XPC connection and added NSXPCConnection to my daemon that shares a dedicated protocol with the server implemented in the UI side using NSXPCListener.

I'd be happy to get reviews and suggestions about my design, especially in the followed aspects

1. Should I implement the UI as launchAgent 
(I need to support multi-users, meaning that the daemon can send
different messages to each UI instance per user) 

.

2. should I consider using dedicated `XPC service`. 
Seems like Xcode has option to create XPC service, 
but I don't know how should I "attach" the UI code inside ... 

.

3. is it possible to have XPC client which runs objective-c 
code while the XPC listener will run on swift code ? 

thanks

Zohar81
  • 4,554
  • 5
  • 29
  • 82

2 Answers2

1
  1. It doesn't matter if you implement the UI app as a regular app or a launch agent. Choose what suits better.
  2. Are you considering to have a UI app and an XPC service? This should work.
  3. It works for Swift, Objective-C and mixed-language projects.

Now I wish to establish a uni directional connection between them so that the Daemon can send messages to be displayed by the UI application.

It didn't work for me. Root process couldn't initialize communication with a user process. But it worked vice versa: if the connection is initiated by UI (you instantiate NSXPCConnection in UI, and a listener in Daemon).

julia_v
  • 593
  • 3
  • 18
  • thanks ! per your advice, I've changed the course of the XPC connection and it works ! However, for performance reasons, is there any way to initiate the messages from the server to client, because by design in my project, the daemon is responsible for initiating all the messages, and I wish to avoid sending constant keep-alives from the client (which is now the UI) – Zohar81 Jan 23 '18 at 07:20
  • Glad it helped! Unfortunately, I didn't find any documentation or a way around this problem. As it looks like, given behavior is reproduces when using Mach Ports as well (user process can talk to root, but not the other way around). – julia_v Jan 23 '18 at 08:44
0

I think you should read Apple's Daemons and Services Programming Guide: https://developer.apple.com/library/content/documentation/MacOSX/Conceptual/BPSystemStartup/Chapters/Introduction.html#//apple_ref/doc/uid/10000172i-SW1-SW1

  1. The UI should probably be done as a regular application that communicates with your daemon.

  2. I don't think XPC Services will suit your needs since you said you need to support multiple users.

an XPC service is private, and is available only to the main application that contains it.

  1. Yes. The NSXPCConnection class is what you would probably use and it is found in Foundation, which is compatible with both Swift and Objective-C.