Do I need a helper application to launch my main application at login? - macos

I am creating this macOS application that offers the user the option to launch at login.
I have googled around and found a recipe that everyone is following for centuries, where a helper app is created and then this helper launches at login and from there, launches your main app.
Is that really necessary? Can't the main app launch directly at login?
I don't see any documentation from Apple saying that this scheme is mandatory.
Any thoughts?

It depends:
There is always the option to add the application manually to the Login Items in System Preferences > Users & Groups (no helper app).
Otherwise if the main app is sandboxed the helper app is mandatory.
Otherwise you could add a custom launchd agent which launches the application.

Related

Pros and cons of splitting Cocoa utility app into helper binary + main app binary?

I'm modernizing and sandboxing an old Cocoa utility and considering approaches. The app lives in the menu bar and works in the background, but shows a Dock icon and a configuration window when the icon is clicked.
There are two approaches:
A. A single binary with LSUIElement=YES, using TransformProcessType to show and hide the dock icon as necessary.
B. A helper tool performs the actual app functionality, manages the menu icon and always runs in LSUIElement mode in background. The main app presents the configuration UI and is only launched when necessary.
The app currently does A. I've noticed that many long-running utility apps have separate helper binaries and basically do B. Examples on my Mac include Paste Helper, TimingHelper, Discord Helper, CCC Helper (for Carbon Copy Cloner), 1Password Extension Helper.
I understand that having a separate helper app is conceptually more pure and potentially allows for better separation of sandbox privileges, but it's also harder to implement, so I doubt that's the sole reason why all these apps opted to have a separate helper binary.
So:
What are the pros and cons of A and B, i.e. why do some choose B over A? Is it required to get some functionality these days?
Is it even possible to have a helper tool outlive the main app in a sandboxed Mac App Store app?
What API does one use to make such a helper? The old-style authorisation APIs seem deprecated, and XPC does not seem like it allows a helper app to launch at startup (and even outliving the main app may be hacky)?
I suspect the reason so many developers choose option B is because this arrangement is now baked into macOS via the "Login Items" facility.
In a nutshell, your main application embeds a second (helper) app and that app is configured as a "Login Item". macOS detects this and automatically adds your helper app to the user's login items. You can control this, programmatically, using SMLoginItemSetEnabled(...).
You end up with a regular app users are familiar with, and a helper app that automatically starts at login and can run in the background. I'm also pretty sure this includes a free XPC connection you can take advantage of.
Read all about it in the Adding Login Items section of the infamous Daemons and Services Programming Guide.
I've never done this myself (I currently install background apps as user agents, which I can do because I don't have a sandboxed app), but I did research it for another project and I know a lot of apps do this.
One disadvantage of option A (based on user feedback from my own apps) is that the main app won't act like a regular app. Using the A approach, your users either can't quit the app (because it will need to automatically restart) or you need a way to hide it in the dock, and then there's no (obvious) way to launch it again. It just gets confusing. If you do let your users quit the app, then the background functionality goes away, and that creates other problems.

OSX: Avoid userprompt when opening embedded binary

I have a sandboxed app that uses an embedded binary to show it's status item.
On first launch of the main app (where it launches the embedded binary like this:
NSWorkspace.sharedWorkspace().launchApplication(statusItemPath)
) OSX displays a user prompt, if the user really wants to start the embedded app:
I find this really confusing for the user - I understand that it is for security reasons but I want to distribute via MAS and so both binaries needs to pass review.
Is there a way to avoid this user prompt (maybe a singing option or entitlement key?)
When an application is downloaded from the internet, or run via another program for the first time, OS X is protecting the user with a mechanism known as 'quarantine'.
Once the user accepts running the application, the quarantine extended attribute on the app is removed.
Removing the quarantine attribute can be done with the following command:
xattr -d com.apple.quarantine /PATH/TO/APPLICATION
So you could call out to the system to run this from your initial application on the embedded binary. However I'm not sure this would be acceptable to Apple for the App Store.
The preferred method would be to use XPC and create a helper app which is launched automatically by launchd. You can read about that here.

Launch at startup and Mac App Store

I've read lots of articles about creating helper tools as LSUIElement and then have them added to LoginItems using SMLoginItemSetEnabled.
What I want to know is, am I allowed to add the main app to LoginItems instead of creating a helper? (And the main app run as a LSUIElement shows in the status bar.)
Since the features were all in the helper and the main app is just a launcher, due to sandbox I can't really validate the receipt within the helper, so I can't prevent people from start the helper directly.
And if it's allowed (I have seem such app but not sure how they did it, such as "Flamingo"), how to add it to LoginItems without have it should down when toggle the settings? As SMLoginItemSetEnabled will terminate the app when pass false.
Thanks.

Launch Application in Mac and Windows

I have my application installed on the user's machine(both windows and mac users). Now the user also goes to my web site for certain other things. I want to provide a link in the web site which says "Launch my app". When he clicks on the link, it should launch my application, which has been installed on his system. I was thinking of going the Active X route but it is not recommended + have to use another approach for Mac. The other option was some how get a file association with my application and make it work. Is there any other option ?
You could register your application for special URL schemas, like mygreatapp://open. In the same way that an ftp://... link opens the default FTP app and a mailto://... link opens the default email application, you can register your own app for any custom [myuniqueschemahere]:// schema.
Register your app as a "helper" app for browsers as part of the installer process.
So if you have a link on your website with a URL like "prashant://thisfineapp", clicking on it launches your application.

Autostart Mac application programmatically

We have a cross platform application. The application has a feature to autostart it once the user logs in. How to do this in mac? from within the application. Manually adding it Login Items works but I am looking for how to do it using an API or something similar.
If it's a GUI app adding it as a login item is the best way to go. Apple's dev note on the subject lists 3 ways to do this: with the Shared File Lists API, via Apple Events, or with the CFPreferences API.
You have to create a launchd property list file and place it in ~/Library/LaunchAgents or /Library/LaunchAgents, depending if you want the change system-wide or only for the current user.
This guide from Apple will help you accomplish that task.

Resources