0
대화식으로 실행되는 cli 앱을 실행하기 위해 NSTask를 사용하고 있습니다. 그것을 사용하여 USB 연결을 통해 데이터를 가져옵니다.NSTask를 사용하는 명령에 대한 대화식 액세스
윤곽선과 오류 파이프가있는 NSTask를 열지 만 명령을 실행하면 작동하지만 출력에서 데이터를 가져 오는 루프에서 비치 볼을 회전합니다. help 명령을 실행 (ID) 보낸 버튼을 다시 출력을 얻을 :
NSTask *usbCommandTask;
NSPipe *outPipe;
NSPipe *inPipe;
NSPipe *errorPipe;
NSFileHandle *inFile;
NSFileHandle *outFile;
NSTimer *pollTimer;
dispatch_queue_t mtpTask;
@implementation AppDelegate
- (void)commandNotification:(NSNotification *)notification
{
NSData *data = nil;
while ((data = [outFile availableData]) && [data length]){
NSString *myString = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
NSLog(@"Data: %@",myString);
}
NSLog(@"Execution never gets here");
}
-(void)checkTask
{
if(usbCommandTask){
if([usbCommandTask isRunning])NSLog(@"Task running"); else NSLog(@"Task dead");
} else NSLog(@"Task is nil");
}
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification {
usbCommandTask = [[NSTask alloc] init];
[usbCommandTask setLaunchPath:@"/Applications/usb-debugger-cli"];
[usbCommandTask setCurrentDirectoryPath:@"/"];
[usbCommandTask setArguments:[NSArray arrayWithObject:@"-i"]];
inPipe = [NSPipe pipe];
[usbCommandTask setStandardInput:inPipe];
outPipe = [NSPipe pipe];
[usbCommandTask setStandardOutput:outPipe];
errorPipe = [NSPipe pipe];
[usbCommandTask setStandardError:errorPipe];
outFile = [outPipe fileHandleForReading];
inFile = [inPipe fileHandleForWriting];
[outFile waitForDataInBackgroundAndNotify];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(commandNotification:)
name:NSFileHandleDataAvailableNotification
object:nil];
pollTimer=[NSTimer scheduledTimerWithTimeInterval:1.0f target:self selector:@selector(checkTask) userInfo:nil repeats:TRUE];
[usbCommandTask launch];
NSLog(@"Launched");
}
- (void)applicationWillTerminate:(NSNotification *)aNotification {
// Insert code here to tear down your application
if(usbCommandTask)[usbCommandTask terminate];
}
- (IBAction)clicked:(id)sender {
if(usbCommandTask){
NSString *[email protected]"help\n";
NSData *commandData=[NSData dataWithBytes:command.UTF8String length:command.length];
[inFile writeData:commandData];
}
else
{NSLog(@"Comamnd task dead");
}
}