r/iOSProgramming • • 4h 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.

2 Upvotes

3 comments sorted by

1

u/Iamvishal16 2h ago

One more gotcha with this: don't store the toggle in UserDefaults and trust it. The user can flip it off in Login Items whenever, so read SMAppService.mainApp.status every time you show your settings window and drive the checkbox from that. Otherwise your UI says it's on while macOS says it isn't, and people file bug reports about it. Also treat .requiresApproval as not enabled in the UI, with a small hint that links to the Login Items pane.

1

u/ThanhDongNguyen 2h ago

Oh, thanks

2

u/dawedev 3h ago

Worth adding for testing: if you toggle it from a debug build, the copy in DerivedData can be the one that gets registered, and then login launches the wrong app. sfltool dumpbtm shows the exact path that's registered, and sfltool resetbtm wipes the list if it gets messy (needs a restart).