Bisquitue Bisquitue - 1 year ago 145
Objective-C Question

See NSTask output Cocoa

How could I make an if statement that's like:

if(the output of a nstask is equal to a specific string){
//do stuff over here

I'm running a
and it put's out the data that's coming from it in the NSLog, but how could I not show it there but store it as a NSString or something like that

This is what my task looks like

NSTask *task = [[NSTask alloc] init];
[task setLaunchPath:@"/usr/bin/csrutil"];
[task setArguments:@[ @"status" ]];
[task launch];

Any help is greatly appreciated :)

Answer Source

You need a pipe and a file handle to read the result.

Write a method

- (NSString *)runShellScript:(NSString *)cmd withArguments:(NSArray *)args
    NSTask *task = [[NSTask alloc] init];
    [task setLaunchPath:cmd];
    [task setArguments:args];

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

    [task launch];

    NSData *data = [file readDataToEndOfFile];
    return [[NSString alloc] initWithData: data encoding: NSUTF8StringEncoding];

and call it

NSString *result = [self runShellScript:@"/usr/bin/csrutil" withArguments:@[ @"status" ]];
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download