Swift 3 OS X app launch on startup
Asked Answered
S

1

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?

Self answered 8/3, 2017 at 18:2 Comment(8)
Possible duplicate of Make Swift Cocoa app launch on startup on OS X 10.11Nevski
No, this isn't my post.Self
Clarified that auto-launch is the goal.Miyamoto
@Self agreed that isn't your post, but it seems to answer your question.Miyamoto
@Miyamoto I have already see this post but the solution use old version of Swift and doesn't work with Swift 3 I have alrealdy try :(Self
@Self Actually, the solution there doesn't show any code; it's just links.Miyamoto
What do you mean ?Self
You can use this library github.com/sindresorhus/LaunchAtLoginSorrento
F
4

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.

Favus answered 9/6, 2017 at 11:41 Comment(1)
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: theswiftdev.com/2017/10/27/how-to-launch-a-macos-app-at-loginFaun

© 2022 - 2024 — McMap. All rights reserved.