I have an app which contains a bundled launch agent that I register using SMAppService.agent(plistName:)
. I’ve packaged the launch agent executable in the typical Mac app bundle structure so I can embed a framework in it. So, the launch agent lives in Contents/SharedSupport/MyLaunchAgent.app/Contents/MacOS/MyLaunchAgent
.
However, I suspect this approach might be falling afoul of the scheduler, since the taskinfo
tool reports my launch agent has a requested & effective role of TASK_DEFAULT_APPLICATION (PRIO_DARWIN_ROLE_UI)
, rather than the TASK_UNSPECIFIED (PRIO_DARWIN_ROLE_DEFAULT)
value I see with system daemons.
I tried setting the LSUIElement
Info.plist key of my launch agent to YES
, but this seems to have had no effect.
What’s the recommended approach here?
I'm not really sure what the difference between a Login Item and a Launch Agent would be.
There are some key differences:
-
launchd
agents can publish named XPC endpoints. -
launchd
agents can use thelaunchd
run-on-demand lifecycle. Login items launch at login and that’s that. -
One important upshot of the previous point is that the system is able to relaunch a
launchd
agent that crashes.
There are good reasons to package a launchd
agent in an app-like wrapper. The most important is that it’s the only way for your agent to use restricted entitlements, that is, entitlements that must be authorised by a provisioning profile. See Signing a daemon with a restricted entitlement, which discusses this in terms of daemons but the same basic logic applies to agents.
Having said that, this packaging isn’t necessary if you use SMAppService
to install your agent. In that case the agent is embedded within your app [1], so it can access resources in one of two ways:
-
It can navigate up the file system hierarchy to access resources from the app’s bundle.
-
It can call a framework, which can have its own resources.
Share and Enjoy
—
Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"
[1] Technically this isn’t required, but it’s the standard approach.