objective-cmacosembedexecutablemac-app-store

Embed Executable inside Mac Application


If you go to /usr/bin you will see hundreds of executables or links to executables.

My application (Mac app written in Obj-C in Xcode) relies on some of these executables. Unfortunately, the executables must be installed manually - I have to check for them and then prompt the user to install them.

My code is:

NSTask *task = [[NSTask alloc] init];
[task setLaunchPath:@"/usr/bin/executable"];

NSPipe *pipe = [NSPipe pipe];
[task setStandardOutput:pipe];
NSFileHandle *file = [pipe fileHandleForReading];

[task setArguments:[NSArray arrayWithObjects:sender, target, nil]];
[task launch];

I was wondering if it possible to copy the executable inside my app somewhere and then call it from there. That way, users wouldn't have to go through getting it for themselves.

And would it be allowed by the Mac App Store?


Solution

  • Ok, so I set out to find the answer for myself by playing around. And, I'm happy to say that I figured it out!

    So, just copy the executable into the Xcode project wherever you want. Then, change

    [task setLaunchPath:@"/usr/bin/executable"];
    

    to

    [task setLaunchPath:[[NSBundle mainBundle] pathForResource:@"executable" ofType:@""]];
    

    And ta da!