The API you're looking for is SMLoginItemSetEnabled()
. You'll have to package your long-running agent as a regular .app bundle and put it into your main app's bundle at Content/Library/LoginItems
.
Then at runtime in the main application you can use the SMLoginItemSetEnabled()
call with the agent's bundle identifier to enable and disable your agent. If your main application quits, the agent stays alive. If the user logs out and back in or reboots, the OS will relaunch your agent when the user logs back in.
Update: As Dmitry notes, the documentation no longer comes with the sample I originally mentioned, but another Apple sample code project, for App Sandbox, demonstrates the same API (see the file NSXPCConnection+LoginItem.m
).