Sanniv Khare 7 min read

SMLoginItemSetEnabled to SMAppService: porting a Mac login item

SMLoginItemSetEnabled to SMAppService: porting a Mac login item

If your Mac app still ships a small helper app in Contents/Library/LoginItems and switches it on with SMLoginItemSetEnabled, the port is short: on macOS 13 and later call SMAppService.mainApp.register() and unregister() for the app itself, keep the old call behind if #available(macOS 13, *) only while you still support macOS 12, and delete the helper target once your minimum is 13. We checked every snippet below with Xcode 26.2 (Swift 6.2.3, macOS 26.2 SDK) on our M1 lab Mac running macOS 15.7.3, at deployment targets 12.0 and 13.0.

Why we did this: OneClick QuitAll and Nix are menu bar utilities, and “Open at login” is one of the first settings people look for in that kind of app. The helper-app route is how most older Mac utilities built it, so it is the code we see most when we pick up an existing Mac project.

What the SDK says now

The macOS 26.2 SDK still ships SMLoginItemSetEnabled, marked deprecated since 13.0. The annotation in SMLoginItem.h reads __OSX_DEPRECATED(10.6, 13.0, "Please use SMAppService instead"), and the comment above it says the API “has been deprecated”. Apple’s reference page lists it for macOS 10.6 to 13.0 and points to register() and unregister() on SMAppService instead.

SMAppService arrived in macOS 13. It covers four kinds of service:

  • SMAppService.mainApp: the app itself opens at login. No helper needed. (docs)
  • loginItem(identifier:): a helper app that still lives in Contents/Library/LoginItems, for apps that really need a separate process at login.
  • agent(plistName:) and daemon(plistName:): launchd plists shipped inside the bundle in Contents/Library/LaunchAgents and Contents/Library/LaunchDaemons, per Apple’s article on updating helper executables.

For a menu bar utility that only wants to start at login, mainApp is the whole answer. The helper existed because the old API could only switch on a helper by bundle ID.

One line in the SMAppService.h header matters for testing: “Apps that use SMAppService APIs must be code signed.” An unsigned local build is fine for compiling, but test register() with a development or Developer ID signed build.

What the compiler says at each deployment target

We wrote three versions of the same toggle (the old call, the new call, and a guarded version that does both) and type-checked each one against macOS 12.0 and 13.0 targets.

swiftc diagnostics for the legacy, modern and guarded login item code at macOS 12.0 and 13.0 targets

Real output from our M1 lab Mac, shortened: the modern file at 12.0 gave four errors, one per API.

What that means in practice:

  • Staying on the old call with a 12.0 target compiles silently. The deprecation warning only appears once you raise the target to 13.0, which is often the first time a team notices.
  • SMAppService without a guard at a 12.0 target is a hard error on the type, on mainApp, on register() and on unregister(). The fix-it the compiler offers is “add ‘if #available’ version check”.
  • The guarded version produced no diagnostics at either target. At 13.0 the old call sits in the else branch of #available(macOS 13, *) and was not flagged.
Side by side: the helper-app call with SMLoginItemSetEnabled and the same toggle with SMAppService.mainApp

The same toggle, before and after. The right-hand side needs no helper bundle at all.

The port, step by step

1. Replace the call

If your minimum is macOS 13 or later, this is the whole change:

import ServiceManagement

func setOpenAtLogin(_ on: Bool) throws {
    let service = SMAppService.mainApp
    if on {
        try service.register()
    } else {
        try service.unregister()
    }
}

Note that the new calls throw. Apple’s register() page documents kSMErrorAlreadyRegistered when the service is already registered and kSMErrorLaunchDeniedByUser when the user has not approved it, so show the error instead of silently flipping the switch back.

2. If you still support macOS 12, guard it

import ServiceManagement

func setOpenAtLogin(_ on: Bool) throws {
    if #available(macOS 13, *) {
        let service = SMAppService.mainApp
        if on {
            try service.register()
        } else {
            try service.unregister()
        }
    } else {
        _ = SMLoginItemSetEnabled(
            "com.example.app.helper" as CFString, on)
    }
}

Keep the helper target and its Copy Files phase into Contents/Library/LoginItems until you drop macOS 12, because the else branch still needs it.

3. Read state from the system, not from your own flag

With the old API, apps usually stored their own “open at login” boolean, because there was no simple way to ask. With SMAppService you can ask. The Status enum has four cases, and our SDK probe printed their raw values:

SMAppService.Status.notRegistered = 0
SMAppService.Status.enabled = 1
SMAppService.Status.requiresApproval = 2
SMAppService.Status.notFound = 3

requiresApproval is the one to design for: the service is registered, but the user has to allow it in System Settings. Show a button that calls SMAppService.openSystemSettingsLoginItems(), which opens the Login Items panel. On current macOS that panel is under System Settings, General, Login Items & Extensions, per Apple Support. Our guide to stopping apps from opening at login shows that panel from the user’s side.

The settings code we built into the test app, for both macOS 12 and 13:

import SwiftUI
import ServiceManagement

enum LaunchAtLogin {
    static let legacyHelperID = "com.ideaxecution.loginlab.helper"

    static var isEnabled: Bool {
        if #available(macOS 13, *) {
            return SMAppService.mainApp.status == .enabled
        }
        return UserDefaults.standard.bool(forKey: "legacyLoginItem")
    }

    static var needsApproval: Bool {
        if #available(macOS 13, *) {
            return SMAppService.mainApp.status == .requiresApproval
        }
        return false
    }

    static func set(_ on: Bool) throws {
        if #available(macOS 13, *) {
            if on { try SMAppService.mainApp.register() }
            else { try SMAppService.mainApp.unregister() }
        } else {
            _ = SMLoginItemSetEnabled(legacyHelperID as CFString, on)
            UserDefaults.standard.set(on, forKey: "legacyLoginItem")
        }
    }
}

A SwiftUI Toggle bound to isEnabled, plus an “Allow in System Settings” button shown when needsApproval is true, completes the screen. A Release build of that app with a macOS 12.0 target finished in 9 seconds on the lab Mac (one clean build, not a benchmark), vtool reported minos 12.0 and sdk 26.2, and the .app was 116 KB. The same project at a 13.0 target also built with no Swift warnings.

4. Drop the helper when your minimum reaches 13

Once macOS 12 is gone from your supported list:

  1. Delete the helper target and the Copy Files phase that put it in Contents/Library/LoginItems.
  2. Remove the else branch and any UserDefaults flag you kept for the old state.
  3. Read SMAppService.mainApp.status wherever the UI shows the setting.

Moving existing users across

The tricky part is people who switched the setting on in an old version. Two things from Apple’s own sources help:

  • For launchd plists installed the old way, SMAppService.statusForLegacyPlist(at:) reports whether that legacy item is still allowed (docs).
  • For a login item switched on with SMLoginItemSetEnabled, the comment on SMCopyAllJobDictionaries in the 26.2 SDK’s ServiceManagement.h says that, although the function is deprecated, it “remains the recommended API” for showing that state to the user, until a replacement arrives.

The simple route we prefer: on the first launch of the version that switches to mainApp, if your stored preference says the user had it on, call register() once, then forget the stored flag and read status from then on. If register() throws or returns requiresApproval, show the approval button rather than a silent failure.

What to check on your own app

  • Search the project for SMLoginItemSetEnabled, LoginItems Copy Files phases and any helper target with “Launcher” or “Helper” in its name.
  • Build once with your real minimum and once with 13.0 to see which warnings and errors you will get.
  • Test the toggle with a signed build, on a user account where the item has not been approved yet, so you see the requiresApproval path.
  • Check the System Settings panel after turning it on and off: the app should appear and disappear there.

Made by the team behind this post

Apps from our studio

  • Goes with this post

    Nix

    The Mac uninstaller that finds what the Trash leaves behind: caches, logs, derived data and saved states.

    macOS

    Get
  • Goes with this post

    OneClick QuitAll

    Quit every running app in one click. See memory and CPU first.

    macOS · Mac App Store

    Get
  • Cardigami

    Pop-up greeting cards that rise out of the fold on iPhone Duo.

    iPhone · App Store

    Get
  • BMDB.fun

    Your Bollywood world at a glance, with links straight to the OTT app that streams each film.

    Android TV · Web · Mobile

    Get

Need this built? Let’s talk.

We build and run apps for Mac, TV, mobile and the web, and write about what we learn.