Close

Add XPC Service

A project log for macOS Temperature Monitor

A temperature monitor for macOS 10.15+ using SwiftUI

rob-grizzardRob Grizzard 02/22/2020 at 18:020 Comments

I started adding an XPC Service [1] by using XCode's built-in functionality.

At this point, I consulted the excellent blog post [2] by Matthew Miner, in which the process of converting the boilerplate [3] Objective-C code into Swift is described.  Interested readers should consult the original source, but the steps are repeated here.

  1. Rename files:
    main.m -> main.swift
    CPU_Temp_XPC.h -> CPU_Temp_XPC.swift
    CPU_Temp_XPC.m -> CPU_Temp_XPC_Delegate.swift
    CPU_Temp_XPCProtocol.h -> CPU_Temp_XPCProtocol.swift
  2. Add the files to the target's "Compile Sources" build phase.
  3. Replace the Objective-C contents with Swift translations and edit build settings:

    1. Install Objective-C Compatibility Header: NO
    2. Objective-C Generated Interface Header Name: “”
    3. Runpath Search Paths: @loader_path/../../../../Frameworks
    4. Swift Language Version: Swift 5
  4. Add testing logic [4]
    1. NOTE - the NSXPCConnection used for testing must use the target's bundle identifier
    2. This commit adds a class, XPC_Tester, and some front-end logic to test that the XPC Service is configured properly.  The test class conforms to the ObservableObject protocol and publishes a variable for tracking by the front-end.  The front-end in turn calls the tester to convert an input String to uppercase using the XPC Service.



[1] https://developer.apple.com/library/archive/documentation/MacOSX/Conceptual/BPSystemStartup/Chapters/CreatingXPCServices.html#//apple_ref/doc/uid/10000172i-SW6-SW1
[2] https://matthewminer.com/2018/08/25/creating-an-xpc-service-in-swift.html
[3] https://github.com/grizzardrl/macOS-temperature-monitor/commit/b7936595c879250a03e8c1f551d98bf62c6d2908
[4] https://github.com/grizzardrl/macOS-temperature-monitor/commit/35193d6eda99437689615a212d99ad6ceec94906

Discussions