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.