Cocoa/ Objective-C Shell Command Line Execution

后端 未结 3 1298
一向
一向 2020-12-02 21:03

This is probably a stupid question, but how can I execute a shell command from my Cocoa app?

I have the command as a string \"command\", but can easily manipulate da

3条回答
  •  感情败类
    2020-12-02 21:41

    NSTask is pretty easy to do this with. For a synchronous call, you can use something like this fragment:

    NSString *path = @"/path/to/executable";
    NSArray *args = [NSArray arrayWithObjects:..., nil];
    [[NSTask launchedTaskWithLaunchPath:path arguments:args] waitUntilExit];
    

    The -waitUntilExit call makes sure it finishes before proceeding. If the task can be asynchronous, you can remove that call and just let the NSTask do it's thing.

提交回复
热议问题