Anoop Vaidya
Anoop Vaidya

Reputation: 46543

Communication between Cocoa App and console app

I have two applications, one with UI (a Cocoa app). The other being a console app.

My requirement is to take user input in CocoaApp and pass it to process to ConsoleApp and return the value.

I have tried something like this, I am able to send and process the data, but not able to return back. I must not use NSDistibutedNotification. I googled and find NSPipe should work, but I am not able to understand how to achieve and use Pipes, please suggest and help to understand.

My codes are here:

In CocoaApp:

- (IBAction)addClicked:(id)sender {

    if (self.firstNumber.stringValue.length == 0 || self.secondNumber.stringValue.length ==0) {
        NSLog(@"Enter values in both the fields");
        return;
    }

    NSString *a = self.firstNumber.stringValue;
    NSString *b = self.secondNumber.stringValue;

    NSTask *unixTask = [[NSTask alloc] init];

    [unixTask setArguments:@[a, b]];

    [unixTask setLaunchPath:@"/Users/.../ConsoleApp"];//the path

    [unixTask launch];
}

In ConsoleApp:

int main(int argc, const char * argv[]) {
    @autoreleasepool {
        // insert code here...

        NSArray *args = [[NSProcessInfo processInfo] arguments];
        NSLog(@"%@", args);

        if(args.count>1){
            Adder *adderObject = [Adder new];
            adderObject.aInt = [args[1] integerValue];
            adderObject.bInt = [args[2] integerValue];

            NSInteger sum = [adderObject addAwithB];

            NSLog(@"Sum = %ld", sum);

        }
    }
    return 0;
}

Upvotes: 2

Views: 175

Answers (1)

zeppenwolf
zeppenwolf

Reputation: 345

If you want to be code signing / sandbox friendly, and do whatever you want XPC, then it's incredibly complicated, ( at least to me! ), but that doesn't matter because the work's already been done for you/me/us. Google for the sample code project "SMJobBlessXPC", based on the "SMJobBless" sample code... It works, I've run it myself, although it's a bit of a pain in the boobs-- make sure the Helper's plist has your correct code signing "common name" in the "Tools owned" key, and that all names of the Helper match up in the Helper tool and the App's Info.plist, etc. If you DON'T need signing/boxing, then I guess you could still use the sample code as a template, just uncheck the signed/boxed aspects of the project build.

Upvotes: 0

Related Questions