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?
Asked
Active
Viewed 2,757 times
7
-
2Possible duplicate of [Make Swift Cocoa app launch on startup on OS X 10.11](http://stackoverflow.com/questions/35339277/make-swift-cocoa-app-launch-on-startup-on-os-x-10-11) – Adolfo Mar 08 '17 at 18:55
-
No, this isn't my post. – Musyanon Mar 08 '17 at 19:27
-
Clarified that auto-launch is the goal. – Richard Mar 09 '17 at 03:12
-
1@Musyanon agreed that isn't your post, but it seems to answer your question. – Richard Mar 09 '17 at 03:12
-
@Richard 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 :( – Musyanon Mar 09 '17 at 06:40
-
@Musyanon Actually, the solution there doesn't show any code; it's just links. – Richard Mar 16 '17 at 19:52
-
What do you mean ? – Musyanon Mar 19 '17 at 10:43
-
1You can use this library https://github.com/sindresorhus/LaunchAtLogin – Ahmadreza Dec 27 '20 at 10:23
1 Answers
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 SMLoginItemSetEnabled
for 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