r/iOSProgramming • u/ThanhDongNguyen • 39m ago
Tutorial Open at login on macOS 13+: SMAppService is enough, you don't need a helper app
I used to look up the old login item helper pattern every time I needed "open at login" in a Mac app. Since macOS 13 you can do it with SMAppService in the ServiceManagement framework, for the main app, with no helper target.
import ServiceManagement
func setLaunchAtLogin(_ on: Bool) throws {
if on {
try SMAppService.mainApp.register()
} else {
try SMAppService.mainApp.unregister()
}
}register() does not always mean it is enabled. Read SMAppService.mainApp.status after you call it.
.enabled— it will launch at login..requiresApproval— the system accepted the registration, but the user still has to allow it in System Settings. This also happens if they turned it off later..notRegistered/.notFound— it is not set up.
If the status is .requiresApproval, you can open the right Settings pane for them:
if SMAppService.mainApp.status == .requiresApproval {
SMAppService.openSystemSettingsLoginItems()
}On my Mac that pane is under System Settings > General > Login Items (the exact name is "Login Items & Extensions" on some versions). I would confirm the label before you show it in a screenshot in your own UI.
You don't need a special entitlement for SMAppService.mainApp. You only need a helper (and SMAppService.agent(plistName:) or the daemon variant) if you are registering something other than the main app.
I think the main mistake is to treat register() as "it is on now" and not handle .requiresApproval. The toggle in your Settings window will look broken if you don't.
Happy to hear if this behaves differently on 26/27 for anyone.
