0

I would like to achieve simple thing but struggling for the whole day. :(

I made a simple macOS app. In AppDelegate, applicationDidFinishLaunching, I would like to execute a helper program which is bundled together inside the app.

https://github.com/hkjlee109/objc-helper-example

But I am getting an error.

Load failed: 5: Input/output error
Try running `launchctl bootstrap` as root for richer errors.

Here's what I did: 0. From

  1. I've a target called "helper-agent" which is a commandline application.
  2. For "helper-agent" application, I've added "com.example.helper.agent.plist" file.
<?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>com.example.helper.agent</string>
    <key>BundleProgram</key>
    <string>Contents/Resources/helper-agent</string>
    <key>MachServices</key>
    <dict>
        <key>com.example.helper.agent</key>
        <true/>
    </dict>
</dict>
</plist>

And I've added two things for "Build phase" of main application.

  1. Copy Bundle Resources: Copy "helper-agent" application to "Contents/Library/LaunchAgents"
  2. Copy Files: Copy "com.example.helper.agent.plist" to "Contents/Library/LaunchAgents"

Lastly, in AppDelegate, I am using below code to launch the helper.

    NSTask* task = [NSTask new];
    task.launchPath = @"/bin/launchctl";
    task.arguments = @[@"load", @"com.example.helper.agent.plist"];
    [task launch];
    [task waitUntilExit];

I would be appreciated for any piece of advices or suggestions. Thanks!

https://github.com/hkjlee109/objc-helper-example

3
  • 1
    Does your app have permission to run launchctl? Second, I think you need the excact path for your .plist, add that from your bundle. Commented Apr 2, 2024 at 6:55
  • No, I haven't thought about the permission. I will do some search on how to grant root permission for my app. Thanks for the comment! Commented Apr 3, 2024 at 7:39
  • 1
    If you just want to test it, you can edit the scheme and run the app as root. Otherwise perhaps look at developer.apple.com/documentation/security/… Commented Apr 3, 2024 at 9:33

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.