7

I just wrote my first application in Swift 3 for OS X. How can I configure this app to launch when the user logs in?

Musyanon
  • 211
  • 1
  • 3
  • 11

1 Answers1

3

This worked for me, but there are some restrictions. First, the application must reside in the Applications folder. Second, I've seen it recommended that a "helper" app be included in the target app bundle to perform the actual startup… but I must admit that I don't know why that is necessary.

if SMLoginItemSetEnabled(appBundleIdentifier as CFString, true) {
    log.info("Successfully added login item.")
} else {
    log.warning("Failed to add login item.")
}

The last argument tells the system whether to add (true) or remove (false) the application from startup. Google SMLoginItemSetEnabledfor more details. Otherwise I am wrestling with this myself and I'd welcome someone to expand on this.

Owen Godfrey
  • 3,361
  • 2
  • 22
  • 18
  • Applications are not able to use their own bundle identifier in this way, so that is why many choose to use a second target. Reference: https://theswiftdev.com/2017/10/27/how-to-launch-a-macos-app-at-login/ – Alec O Nov 12 '18 at 18:48