Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Swift 3 OS X app launch on startup

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?

like image 575
Musyanon Avatar asked Oct 18 '22 16:10

Musyanon


1 Answers

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.

like image 68
Owen Godfrey Avatar answered Oct 21 '22 08:10

Owen Godfrey