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.
- 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 - Add the files to the target's "Compile Sources" build phase.
Replace the Objective-C contents with Swift translations and edit build settings:
- Install Objective-C Compatibility Header: NO
- Objective-C Generated Interface Header Name: “”
- Runpath Search Paths: @loader_path/../../../../Frameworks
- Swift Language Version: Swift 5
- Add testing logic [4]
- NOTE - the NSXPCConnection used for testing must use the target's bundle identifier
- 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
Become a Hackaday.io Member
Create an account to leave a comment. Already have an account? Log In.