I'm trying to create a very simple project where an application contains a helper application and the helper application is defined as a LaunchAgent by the main application.
So far, when I call agentServiceWithPlistName:, and then request the status, I get the value 3 (i.e.SMAppServiceStatusNotFound).
I checked that the main .app bundle did not have obvious issues:
- there is definitely a .plist in Contents/Library/LaunchAgents.
- the .plist definitely defines the BundleProgram value.
- there is definitely a .app helper application and the relative path pointed by the BundleProgram key looks definitely correct.
[Q] Are there some useful logs provided by the SMAppService APIs/framework that can provide an idea why a service is not found?
I haven't seen any so far in Console.app. I have not seen so far a hint in the documentation that would suggest that this SMAppService mechanism requires an app to be notarized or codesigned with a level above (Run locally).
Extra Question: Is there an official example for these new APIs?
then request the status, I get the value 3
I think you’re being mislead by that status value. It doesn’t mean that things have gone wrong, but rather than the system knows nothing about your agent yet. That is, status
is returning information about the service as it’s installed in the system, not about your property list.
Is there an official example for these new APIs?
Not that I’m aware of. Having said that, assuming a familiarity with launchd
agents, I’ve found that this API is pretty straightforward to start out with. Indeed, I just took it for a spin:
-
I created a new Mac app test project.
-
Within that, I created an agent target from the command-line tool template.
-
I changed the code to look like this:
import Foundation func main() { while true { print("Hello Cruel World!") sleep(10) } } main()
-
I added an Embed Helper Tools build phase, per the Embed the helper tool section of Embedding a command-line tool in a sandboxed app.
-
I added a
com.example.apple-samplecode.Test721737.agent.plist
property list to the project. -
I changed it to look like this:
… <plist version="1.0"> <dict> <key>Label</key> <string>com.example.apple-samplecode.Test721737.agent</string> <key>BundleProgram</key> <string>Contents/MacOS/com.example.apple-samplecode.Test721737.agent</string> </dict> </plist>
-
I removed it from the app’s Copy Bundle Resources build phase and added it to a new Copy Agent Property Lists build phase, targeting
Contents/Library/LaunchAgents
. -
In the app I wired up a button to run this code:
let service = SMAppService.agent(plistName: "com.example.apple-samplecode.Test721737.agent.plist") @IBAction private func registerAction(_ sender: Any) { do { print("will register, status: \(service.status.rawValue)") try service.register() print("did register") } catch { print("did not register, error: \(error)") } }
-
I built the app. It’s structure looks like this:
% find Test721737.app Test721737.app Test721737.app/Contents Test721737.app/Contents/_CodeSignature Test721737.app/Contents/_CodeSignature/CodeResources Test721737.app/Contents/MacOS Test721737.app/Contents/MacOS/com.example.apple-samplecode.Test721737.agent Test721737.app/Contents/MacOS/Test721737 Test721737.app/Contents/Resources Test721737.app/Contents/Resources/MainMenu.nib Test721737.app/Contents/Library Test721737.app/Contents/Library/LaunchAgents Test721737.app/Contents/Library/LaunchAgents/com.example.apple-samplecode.Test721737.agent.plist Test721737.app/Contents/Info.plist Test721737.app/Contents/PkgInfo
-
I clicked on the button to register the agent:
will register, status: 3 did register
-
In Terminal, I saw the agent registered:
% launchctl list | grep com.example.apple-samplecode.Test721737.agent - 0 com.example.apple-samplecode.Test721737.agent
-
The agent isn’t running because it’s configured to launch on demand, and there is no demand. I manually start it and then confirmed that it’s now running:
% launchctl start com.example.apple-samplecode.Test721737.agent % launchctl list | grep com.example.apple-samplecode.Test721737.agent 53068 0 com.example.apple-samplecode.Test721737.agent
Share and Enjoy
—
Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"