NSTask and NSPipe example to comunicate with the command line objective-c

 ̄綄美尐妖づ 提交于 2019-12-03 16:13:05

Apple have some nice sample code that shows how to do most of that... http://developer.apple.com/mac/library/samplecode/Moriarity/

TaskWrapper.m contains all the clever stuff, but since you want to be able to send data to the task, you'll need to extend it a little, like so:

[task setStandardInput: [NSPipe pipe]];

To send input to the task, you can then do:

[[[task standardInput] fileHandleForWriting] writeData: ...];

To turn the NSTextField's value into data, you can do something like this:

NSData *data = [[inputTextField stringValue] dataUsingEncoding:NSUTF8StringEncoding];

...and to set the current working directory for your sub-task, use [NSTask setCurrentDirectoryPath:]

e.g.

[task setCurrentDirectoryPath:@"/blah/blah"];
[task setLaunchPath:@"/blah/blah/server.sh"];

.... (other setup code)

[task launch];

There's also AMShellWrapper sample code which improves on moriarity.

http://www.harmless.de/cocoa-code.php

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!