0

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 NSTask 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 :)

1 Answers1

2

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" ]];
vadian
  • 274,689
  • 30
  • 353
  • 361
  • thanks for the help but how could I make an if statement if result is equal to a string? –  Aug 25 '16 at 17:24
  • [How to check if NSString equals a specific string value?](http://stackoverflow.com/q/7266218) @Bisquitue – jscs Aug 25 '16 at 18:55
  • @JoshCaswell That was a stupid question I just realized it after I posted it –  Aug 25 '16 at 19:15