FB5418390: Add ability to check the status of `SMLoginItemSetEnabled` ("Launch at login" functionality)
sindresorhus opened this issue · comments
- Date: 2018-10-11
- Resolution: Fixed
- Area: AppKit
- OS: macOS 10.14
- Type: Suggestion
Description
Sandboxed apps must use the SMLoginItemSetEnabled
method to enable/disable enable an app to be launched at login (through a helper app). Usually, you would have a checkbox in the app's preferences to toggle it, like [ ] Launch at login
. The problem is that there is no way to know whether the helper app is set to be launched at login or not. So you can't correctly show the status in the checkbox. The SMLoginItemSetEnabled
method only lets us set the status, but not check it. We could persist the status to UserDefaults, but then it could easily get out of sync (Has happened to my app before). A common workaround is to use SMCopyAllJobDictionaries
to check the status 0, but that API has been deprecated since macOS 10.10 with no replacement...
Expected Results:
I expected there to be a way to check the current status of SMLoginItemSetEnabled
. There could be a method called SMLoginItemIsEnabled
that would accept a bundle identifier and return a bool of whether it's enabled or not.
Actual Results:
Have to use UserDefaults which can be buggy or a deprecated API (SMCopyAllJobDictionaries).
Notes:
Relevant GitHub issue: sindresorhus/LaunchAtLogin-Legacy#12
I just noticed that the SMCopyAllJobDictionaries
header says:
For the specific use of testing the state of a login item that may have been enabled with SMLoginItemSetEnabled() in order to show that state to the user, this function remains the recommended API. A replacement API for this specific use will be provided before this function is removed.
I'm not sure if this was always there or if it's new.
This was added in macOS 13 with SMAppService: https://developer.apple.com/documentation/servicemanagement/smappservice/3945412-mainapp?changes=latest_minor
🎉