How to launch background application (app bundle) and keep alive it on macOS?

How to launch background application (app bundle) and keep alive it on macOS?


The application has roles of sending notification to notification center on macOS and processing notification actions such as "active".


It should be 1)kept alive and 2)background application.


I've been tried following steps.


1. Adding a "Application is background only" key that has a "YES" value to an application project plist file


2. Check that the app run as an background app, building and launching the app on xcode.


3. The application is registered as LaunchAgent

launch agent plist file key value

<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">

<plist version="1.0">

<dict>

<key>Label</key>

<string>BUNDLEID</string>

<key>ProgramArguments</key>

<array>

<string>MYAPPPATH/MY.app/Contents/MacOS/MYAPP</string>

</array>

<key>RunAtLoad</key>

<true/>

<key>KeepAlive</key>

<true/>

</dict>

</plist>


4. After loading the plist using launchctl, the app is launched. But It is not "background".


5. Try again, registe the app as launchAgent after modifying a plist file referenced from http://stackoverflow.com/questions/18134992/how-can-i-start-a-program-app-with-launchdaemons-launchd


<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">

<plist version="1.0">

<dict>

<key>Label</key>

<string>BUNDLEID</string>

<key>ProgramArguments</key>

<array>

<string>/usr/bin/open</string>

<string>-W</string>

<string>MYAPPPATH/MY.app/Contents/MacOS/MYAPP</string>

</array>

<key>RunAtLoad</key>

<true/>

<key>KeepAlive</key>

<true/>

</dict>

</plist>


6. However, after reloading the plist, the app is still not "background".


Is it possible to satify two that one is background launched app bundle and the other is keep alive that mean when the app is terminated, launch it again.

Answered by DTS Engineer in 207720022

When deploying via Developer ID, there are two ways to create a process that runs continuously while the user is logged in:

  • Via an XPC login item

  • Via a launchd agent

In general I recommend the first option because it’s much easier to set up. Specifically, if you create a launchd agent you have to explicitly put your agent somewhere and then create and load the launchd property list file. OTOH, when creating an XPC login item your code lives within your app’s bundle, so the installation process is much easier (you just call

SMLoginItemSetEnabled
).

You can find an example of this in the AppSandboxLoginItemXPCDemo sample code.

If your XPC login item connects to the window server — and thus has a menu bar, shows up in the Dock, and so on — the correct fix is to set one of the following keys in its

Info.plist
:
  • LSBackgroundOnly
  • LSUIElement

Use the first if the XPC login item has no UI; user the second if it has a UI (like a status item, or a floating window activated by a hot key) but doesn’t want to show up in the Dock.

Note The documentation for these keys is unclear about their type (r. 30162536). It’s correct to use a Boolean value here.

Share and Enjoy

Quinn “The Eskimo!”
Apple Developer Relations, Developer Technical Support, Core OS/Hardware

let myEmail = "eskimo" + "1" + "@apple.com"

OK, first things first, are you planning to deploy via the Mac App Store? Or via Developer ID?

Share and Enjoy

Quinn “The Eskimo!”
Apple Developer Relations, Developer Technical Support, Core OS/Hardware

let myEmail = "eskimo" + "1" + "@apple.com"

I have a plan to deploy my app via Developer ID.

In addition, When I test that "-runMode" option is appended to "ProgramArguments" in plist, my app is launched as background app it is referenced from "google software update"'s plist file in ~/Library/LaunchAgents. Is it a recommanded way?

Accepted Answer

When deploying via Developer ID, there are two ways to create a process that runs continuously while the user is logged in:

  • Via an XPC login item

  • Via a launchd agent

In general I recommend the first option because it’s much easier to set up. Specifically, if you create a launchd agent you have to explicitly put your agent somewhere and then create and load the launchd property list file. OTOH, when creating an XPC login item your code lives within your app’s bundle, so the installation process is much easier (you just call

SMLoginItemSetEnabled
).

You can find an example of this in the AppSandboxLoginItemXPCDemo sample code.

If your XPC login item connects to the window server — and thus has a menu bar, shows up in the Dock, and so on — the correct fix is to set one of the following keys in its

Info.plist
:
  • LSBackgroundOnly
  • LSUIElement

Use the first if the XPC login item has no UI; user the second if it has a UI (like a status item, or a floating window activated by a hot key) but doesn’t want to show up in the Dock.

Note The documentation for these keys is unclear about their type (r. 30162536). It’s correct to use a Boolean value here.

Share and Enjoy

Quinn “The Eskimo!”
Apple Developer Relations, Developer Technical Support, Core OS/Hardware

let myEmail = "eskimo" + "1" + "@apple.com"

I've tested "SMLoginItemSetEnabled". It is OK. Thank you!🙂

How to launch background application (app bundle) and keep alive it on macOS?
 
 
Q