Honoring User's Changed Selection when Registering macOS MainApp with SMAppService

Hello, Currently my macOS application registers itself as a login item in the AppDelegate applicationDidFinishLaunching method (see code below) However, I'm running into a problem that if the user is auto upgraded (internal 3rd party implementation) that the .pkg postinstall script runs, the last step which is launching the GUI application. Because of this, if a user unselects our app as a LoginItem, when it is relaunched, it will add itself back. I have checked the SMAppService statuses (.enabled, .notRegistered, .notFound) and discovered that when a user disables the app as a login item, the status is returned as .notFound. I am trying to find a way to detect if the user previously removed our app from login items and not register the app as a login item back, but for the first time the user opens the app the app is registered as a login item. Would checking if the status is .notRegistered work in this case for a first time install? What should i do differently?

func applicationDidFinishLaunching(_ aNotification: Notification) { ...

    guard !Runtime.isDebug else {
        self.logger.debug("Detected Xcode host; Skipping installation of helper components.")
        return
    }

    self.logger.info("Setting UI login item")
    if mainApp.status != .enabled { //old code, incorrect. What should go here?
        do {
            try mainApp.register()
        } catch {
            logger.error("Failed to initialize UI login item: \(error.localizedDescription)")
        }
    }
} 

Lemme see if I have this straight:

  • You want to automatically register your login item.

  • But still allow the user to disable it.

Is that right? What’s your rationale for this approach?

Note that this is a weird combination. More typical use cases are:

  • The app puts this entirely under the control of the user. That is, it doesn’t automatically register the login item in the first place [1].

  • The app really wants the login item to run, and thus always overwrites the user’s preference.

It should go without saying that I recommend the first rather than the second approach.

Share and Enjoy

Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"

[1] Indeed, that’s the approach required by the Mac App Store (see 2.4.5(iii) of the App Review Guidelines) but I recognise that you’re not deploying that way.

The rationale for the approach is that we are on pace to become a required third party tool for the organization.

Agree to the point of letting the user choose if the app should run as a login item. I have seen other apps offer a notification to allow the user choice to add as a login item. Is there a hook to detect if a sanboxed app is opened as a login item instead of manually that we can detect? We are a 3rd party tool installed directly thru .pkg

Honoring User's Changed Selection when Registering macOS MainApp with SMAppService
 
 
Q