Macos 使用NSPipe和NSTask模拟命令行输入
我正在为命令行应用程序编写一个非常简单的GUI工具。它只有两个按钮连接和退出Macos 使用NSPipe和NSTask模拟命令行输入,macos,cocoa,Macos,Cocoa,我正在为命令行应用程序编写一个非常简单的GUI工具。它只有两个按钮连接和退出 在ApplicationIDFinishLaunching中,我运行以下命令 NSPipe *pipe = [[NSPipe alloc] init]; writer = [pipe fileHandleForWriting]; NSTask *runTask = [[[NSTask alloc] init] autorelease]; NSString *exefile = [[[NSBundle mainBund
在
ApplicationIDFinishLaunching
中,我运行以下命令
NSPipe *pipe = [[NSPipe alloc] init];
writer = [pipe fileHandleForWriting];
NSTask *runTask = [[[NSTask alloc] init] autorelease];
NSString *exefile = [[[NSBundle mainBundle] resourcePath]
stringByAppendingPathComponent:@"vpngui"];
[runTask setLaunchPath: exefile];
NSString *exeDir = [[NSBundle mainBundle] resourcePath];
NSArray *pargs;
pargs = [NSArray arrayWithObjects: exeDir, nil];
[runTask setArguments: pargs];
[runTask setStandardInput:pipe];
[runTask launch];
然后,单击连接按钮时,将运行以下代码行
writer writeData:[@"Connect" dataUsingEncoding:NSUTF8StringEncoding]];
对于退出按钮
writer writeData:[@"Quit" dataUsingEncoding:NSUTF8StringEncoding]];
不知何故,命令行应用程序从未获得连接和退出命令,数据可能已缓冲 尝试通过调用以下方法强制刷新文件句柄: 同步文件 使接收器表示的文件的所有内存中数据和属性写入永久存储器
我遇到了同样的问题并且 对我有用 关闭文件:表示您已完成编写
但是,下次要写入时,必须创建一个新的fileHandle实例,因为旧的实例已关闭。尝试在写入的字符串中添加一个换行符。。。在换行符上缓冲shell管道并不少见。请改用
NSLog()
语句,查看是否发生任何情况或设置断点。添加[writer synchronizeFile];结果:[NSConcreteFileHandle synchronizeFile]:不支持操作