最简service

  1. static dispatch_queue_t process_event_queue(void) {
  2. static dispatch_queue_t _queue;
  3. static dispatch_once_t onceToken;
  4. dispatch_once(&onceToken, ^{
  5. _queue = dispatch_queue_create("com.xxx.process.event.queue", DISPATCH_QUEUE_SERIAL);
  6. });
  7. return _queue;
  8. }
  9. void event_handler(xpc_connection_t peer, xpc_object_t event) {
  10. NSDictionary* info = [NSDictionary dictionaryWithXPCObject:event];
  11. xpc_object_t reply = [info XPCObject];
  12. xpc_connection_send_message(peer, reply);
  13. }
  14. int main(int argc, const char * argv[]) {
  15. @autoreleasepool {
  16. xpc_connection_t service =
  17. xpc_connection_create_mach_service("com.xxx.uem.uem-agent",
  18. dispatch_get_main_queue(),
  19. XPC_CONNECTION_MACH_SERVICE_LISTENER);
  20. if (service) {
  21. xpc_connection_set_event_handler(service, ^(xpc_object_t _Nonnull peer) {
  22. xpc_type_t type = xpc_get_type(peer);
  23. if (type == XPC_TYPE_CONNECTION) {
  24. // TODO: validate the peer connection
  25. xpc_connection_set_target_queue(peer, process_event_queue());
  26. xpc_connection_set_event_handler(peer, ^(xpc_object_t _Nonnull object) {
  27. event_handler(peer, object);
  28. });
  29. xpc_connection_resume(peer);
  30. }
  31. });
  32. xpc_connection_resume(service);
  33. dispatch_main();
  34. }
  35. }
  36. return 0;
  37. }

最简client

  1. #import <Foundation/Foundation.h>
  2. #import "ObjectToXPC/ObjectToXPC.h"
  3. int main(int argc, const char * argv[]) {
  4. @autoreleasepool {
  5. xpc_connection_t connection;
  6. connection = xpc_connection_create_mach_service("com.xxx.uem.uem-agent",
  7. NULL,
  8. XPC_CONNECTION_MACH_SERVICE_PRIVILEGED);
  9. if (connection) {
  10. xpc_connection_set_event_handler(connection, ^(xpc_object_t _Nonnull object) {
  11. if (xpc_get_type(object) == XPC_TYPE_ERROR) {
  12. NSLog(@"XPC Error: %s", xpc_copy_description(object));
  13. } else {
  14. NSDictionary* info = [NSDictionary dictionaryWithXPCObject:object];
  15. NSLog(@"info: %@", info);
  16. }
  17. });
  18. }
  19. xpc_connection_resume(connection);
  20. NSDictionary* message = @{@"name" : @"zhaorui"};
  21. xpc_connection_send_message(connection, [message XPCObject]);
  22. dispatch_main();
  23. }
  24. return 0;
  25. }

launchd.plist

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
  3. <plist version="1.0">
  4. <dict>
  5. <key>Label</key>
  6. <string>com.xxx.uem.uem-agent</string>
  7. <key>KeepAlive</key>
  8. <true/>
  9. <key>Program</key>
  10. <string>/Users/xxx/uem-agent/build/Debug/uem-agent</string>
  11. <key>ProgramArguments</key>
  12. <array>
  13. <string>/Users/xxx/uem-agent/build/Debug/uem-agent</string>
  14. </array>
  15. <key>MachServices</key>
  16. <dict>
  17. <key>com.xxx.uem.uem-agent</key>
  18. <true/>
  19. </dict>
  20. </dict>
  21. </plist>

三方库

https://github.com/rastersize/ObjectToXPC