UPGRADE YOUR SKILLS: Learn advanced Swift and SwiftUI on Hacking with Swift+! >>

SOLVED - Add launch at login to macOS app

Forums > macOS

Anyone recently added launch at login for a #macOS app? I've followed this https://jogendra.dev/implementing-launch-at-login-feature-in-macos-apps… but it doesn't seem to work. I notarise my app, launch it and shut down my Mac but after starting again the app doesn't launch. Is this still the way to go?

My app is mainly build with SwiftUI and targets macOS Monterey and would greatly benefit from this feature.

According to the docs the SMLoginItemSetEnabled returns YES if the requested has taken effect. Below does return true for the state, so that should be working I guess. Wonder why it won't start.

.onChange(of: launchAtLogin) { newValue in
    let state = SMLoginItemSetEnabled(Constants.helperBundleID as CFString, newValue)
    print("Setting is enabled \(state)")
}

3      

I've contacted the author of the article a little while back and he helped to figure it out, thought I'd mention it here too. The tutorial in the article works with the following adjustment:

In the applicationDidFinishLaunching function in the launcher target, the following two lines should be replaced:

guard let pathURL = URL(string: actualAppPath) else { return }
NSWorkspace.shared.openApplication(at: pathURL, configuration: NSWorkspace.OpenConfiguration())

With this:

NSWorkspace.shared.launchApplication(actualAppPath)

The latter was deprecated in macOS 11.0 but somehow that solution does still work and the first one doesn't. Not sure it it's future proof for long, perhaps the new API's in macOS Ventura will offer a better solution.

3      

I'll just leave it here

let appPath: String = {
  let path = Bundle.main.bundlePath as NSString
  var components = path.pathComponents
  components.removeLast(4)
  return NSString.path(withComponents: components)
}()
let appURL = URL(fileURLWithPath: appPath)
NSWorkspace.shared.openApplication(at: appURL,
configuration: NSWorkspace.OpenConfiguration(),
completionHandler: nil)

3      

@stpe  

In case someone goes down this path... check out Sindre Sorhus LaunchAtLogin package.

4      

BUILD THE ULTIMATE PORTFOLIO APP Most Swift tutorials help you solve one specific problem, but in my Ultimate Portfolio App series I show you how to get all the best practices into a single app: architecture, testing, performance, accessibility, localization, project organization, and so much more, all while building a SwiftUI app that works on iOS, macOS and watchOS.

Get it on Hacking with Swift+

Sponsor Hacking with Swift and reach the world's largest Swift community!

Reply to this topic…

You need to create an account or log in to reply.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.