Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/iphone/35.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Iphone NSTask实时监控输出_Iphone_Objective C_Macos_Cocoa_Nstask - Fatal编程技术网

Iphone NSTask实时监控输出

Iphone NSTask实时监控输出,iphone,objective-c,macos,cocoa,nstask,Iphone,Objective C,Macos,Cocoa,Nstask,我似乎无法监视NSTask命令的输出。据我所知,必须使用NSNotificationCenter。我试图运行的终端命令使用各种加密方法从我的安全服务器下载一个文件(在objective-c中重写此命令将是一件非常痛苦的事情)。我将需要监测结果,以便我可以收到完成下载的百分比 这是我到目前为止所拥有的 NSNotificationCenter *nc = [NSNotificationCenter defaultCenter]; task = [[NSTask alloc] init]; pi

我似乎无法监视NSTask命令的输出。据我所知,必须使用NSNotificationCenter。我试图运行的终端命令使用各种加密方法从我的安全服务器下载一个文件(在objective-c中重写此命令将是一件非常痛苦的事情)。我将需要监测结果,以便我可以收到完成下载的百分比

这是我到目前为止所拥有的

NSNotificationCenter *nc = [NSNotificationCenter defaultCenter];

task = [[NSTask alloc]  init];
pipe = [[NSPipe alloc] init];

NSDictionary *defaultEnvironment = [[NSProcessInfo processInfo] environment];
NSMutableDictionary *environment = [[NSMutableDictionary alloc] initWithDictionary:defaultEnvironment];
[environment setObject:@"YES" forKey:@"NSUnbufferedIO"];
[task setEnvironment:environment];

[task setLaunchPath:[[NSBundle mainBundle] pathForResource:@"servDecryptor" ofType:nil]];
[task setArguments:[NSArray arrayWithArray:arguments]];
[task setStandardOutput:pipe];

fh = [pipe fileHandleForReading];

[nc addObserver:self
       selector:@selector(ready:)
           name:NSFileHandleReadCompletionNotification
         object:fh];
[nc addObserver:self
       selector:@selector(decFinished:)
           name:NSTaskDidTerminateNotification
         object:task];

[task launch];

[fh readInBackgroundAndNotify];

请注意:下载开始并进行,无需进入我的第二种方法。但是,应用程序在下载完成且过程结束后崩溃

-(void)uploadData
{
setenv([@"PASSWORD" UTF8String], [mPassword UTF8String], 1);
[task setLaunchPath:executablePathRoot];
[task setArguments:array];
NSPipe *pipe = [NSPipe pipe];
NSPipe *errorPipe = [NSPipe pipe];
[task setStandardOutput:pipe];
[task setStandardError:errorPipe];
//keeps your log where it belongs
//[task setStandardInput:[NSPipe pipe]];

NSFileHandle *outFile = [pipe fileHandleForReading];
NSFileHandle *errFile = [errorPipe fileHandleForReading];


[task launch];
[[NSNotificationCenter defaultCenter] addObserver:self
                                         selector:@selector(terminated:)
                                             name:NSTaskDidTerminateNotification
                                           object:task];

[[NSNotificationCenter defaultCenter] addObserver:self
                                         selector:@selector(outData:)
                                             name:NSFileHandleDataAvailableNotification
                                           object:outFile];

[[NSNotificationCenter defaultCenter] addObserver:self
                                         selector:@selector(errData:)
                                             name:NSFileHandleDataAvailableNotification
                                           object:errFile];


[outFile waitForDataInBackgroundAndNotify];
[errFile waitForDataInBackgroundAndNotify];
NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init];
while(!terminated) 
{
    if (![[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode beforeDate:[NSDate distantFuture]]) 
    {
        break;
    }
    [pool release];
    pool = [[NSAutoreleasePool alloc] init];
}
[pool release];

[self appendDataFrom:outFile to:output];
[self appendDataFrom:errFile to:error];
//[task waitUntilExit];
[task release];
}


-(void) outData: (NSNotification *) notification
{
NSLog(@"outData");
NSFileHandle *fileHandle = (NSFileHandle*) [notification object];
[self appendDataFrom:fileHandle to:output];
[fileHandle waitForDataInBackgroundAndNotify]; //Checks to see if data is available in a background thread.
}


-(void) errData: (NSNotification *) notification
{
NSLog(@"errData");
NSFileHandle *fileHandle = (NSFileHandle*) [notification object];
[self appendDataFrom:fileHandle to:output];
[fileHandle waitForDataInBackgroundAndNotify];
}

- (void) terminated: (NSNotification *)notification
{
NSLog(@"Task terminated");
[[NSNotificationCenter defaultCenter] removeObserver:self];
terminated =YES;
}
-(void)uploadData
{
setenv([@"PASSWORD" UTF8String], [mPassword UTF8String], 1);
[task setLaunchPath:executablePathRoot];
[task setArguments:array];
NSPipe *pipe = [NSPipe pipe];
NSPipe *errorPipe = [NSPipe pipe];
[task setStandardOutput:pipe];
[task setStandardError:errorPipe];
//keeps your log where it belongs
//[task setStandardInput:[NSPipe pipe]];

NSFileHandle *outFile = [pipe fileHandleForReading];
NSFileHandle *errFile = [errorPipe fileHandleForReading];


[task launch];
[[NSNotificationCenter defaultCenter] addObserver:self
                                         selector:@selector(terminated:)
                                             name:NSTaskDidTerminateNotification
                                           object:task];

[[NSNotificationCenter defaultCenter] addObserver:self
                                         selector:@selector(outData:)
                                             name:NSFileHandleDataAvailableNotification
                                           object:outFile];

[[NSNotificationCenter defaultCenter] addObserver:self
                                         selector:@selector(errData:)
                                             name:NSFileHandleDataAvailableNotification
                                           object:errFile];


[outFile waitForDataInBackgroundAndNotify];
[errFile waitForDataInBackgroundAndNotify];
NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init];
while(!terminated) 
{
    if (![[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode beforeDate:[NSDate distantFuture]]) 
    {
        break;
    }
    [pool release];
    pool = [[NSAutoreleasePool alloc] init];
}
[pool release];

[self appendDataFrom:outFile to:output];
[self appendDataFrom:errFile to:error];
//[task waitUntilExit];
[task release];
}


-(void) outData: (NSNotification *) notification
{
NSLog(@"outData");
NSFileHandle *fileHandle = (NSFileHandle*) [notification object];
[self appendDataFrom:fileHandle to:output];
[fileHandle waitForDataInBackgroundAndNotify]; //Checks to see if data is available in a background thread.
}


-(void) errData: (NSNotification *) notification
{
NSLog(@"errData");
NSFileHandle *fileHandle = (NSFileHandle*) [notification object];
[self appendDataFrom:fileHandle to:output];
[fileHandle waitForDataInBackgroundAndNotify];
}

- (void) terminated: (NSNotification *)notification
{
NSLog(@"Task terminated");
[[NSNotificationCenter defaultCenter] removeObserver:self];
terminated =YES;
}