Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SMLoginItemSetEnabled returns success but doesn’t add login item

I’m trying to add a login item to a macOS app. The app is set to agent in its plist so it has no dock icon, hides its main window in launch, and places an icon in the status bar to provide its UI.

To add the login item, I have:

  • Created a new macOS app within the project (the launch helper)
  • Disabled Strip Debug Symbols During Copy in the base app
  • Added a Copy Files phase to the base app, destination set to Wrapper, Subpath set to Contents/Library/LoginItems, and added the helper app to the list of files to copy
  • Set the helper app to background only
  • Enabled Skip Install for the helper app
  • Removed the window from the helper app
  • Added code to the helper app’s applicationDidFinishLaunching to launch the base app
  • Added an option to the base app to call SMLoginItemSetEnabled((__bridge CFStringRef)@"com.mydomain.MyApp-Helper", enabled) to allow toggling the login item
  • Enabled sandbox and codesigning for both base and helper app
  • Built the project and copied the app to /Applications
  • Launched the app from /Applications and enabled the option to launch at login

The problem is that although I get feedback from the app that SMLoginItemSetEnabled was successful, the helper app does not run on login, or if it runs, it does not launch the base app.

I looked inside the base app bundle and the helper app is in there. I can run it manually, and it does launch the base app. I can even manually add it as a login item through System Preferences and it still launches the base app. So it would appear that even though SMLoginItemSetEnabled is returning true, it is not actually installing the login item. Or if it is, it’s not able to launch and run the base app from the login item even though it can if I do it manually.

like image 553
mashers Avatar asked Aug 03 '18 20:08

mashers


1 Answers

The solution to this was to delete the DerivedData folder for both the base app and the helper app. It would seem that the login item was confused about which copy of the app to open, and was presumably trying to open one from DerivedData. Deleting these folders and leaving only the app in /Applications allowed the helper app to launch the base app from the login item.

like image 139
mashers Avatar answered Oct 25 '22 03:10

mashers