How to create a login item?

Hey there,


I want a macOS app to use a login item. I found this here, but it's a quite old document, difficult to understand, and not executable because of some errors.


So do you know a suitable way to implement a login item? Preferably it should be installed itself when the user install the application.


Thanks a lot!

Replies

Hi, just recently I also dealt with setting a macOS app as a login item so that it starts automatically when the user logs in.

So do you know a suitable way to implement a login item?

Here's a solution using SMLoginItemSetEnabled to set a login item.
Just follow along this tutorial, it worked fine for me and is up to date, using Swift :)
theswiftdev.com/how-to-launch-a-macos-app-at-login/


Preferably it should be installed itself when the user install the application.

You will create another target inside your project, so it will be installed with your main application.

Keep in mind, that SMLoginItemSetEnabled can be tricky to test on your development computer (I'm speaking from personal experience). You should consider testing your app on a virtual machine with a fresh installation of macOS or on a different Mac, if testing on your development machine fails.
Also make sure, to place your app in the applications folder.

I hope that helps :)
Thank you, I'll try!

Since macOS 13.0 you can use SMAppService to add and remove login items.

Just use


SMAppService.mainApp.register()
SMAppService.mainApp.unregister()