执行“readDataToEndOfFile”方法后,应用程序执行停止/挂起。请找到以下代码,如何解决此问题
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
NSTask *task = [[NSTask alloc] init];
[task setLaunchPath: @"/usr/sbin/tcpdump"];
[task setArguments: [[NSArray alloc] initWithObjects:@"-l", @"-i",@"en1",@"-A",@"-vvv",@"host",@"www.facebook.com", nil]];
NSPipe *pipe= [NSPipe pipe];
[task setStandardOutput: pipe];
NSFileHandle *fileHandle = [pipe fileHandleForReading];
[task launch];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(receivedData:) name:NSFileHandleDataAvailableNotification object:fileHandle];
[fileHandle waitForDataInBackgroundAndNotify];
}
- (void)receivedData:(NSNotification *)notif {
NSFileHandle *fileHandle=notif.object;
NSData *data = [fileHandle readDataToEndOfFile];
NSString *output = [[NSString alloc] initWithData: data encoding: NSUTF8StringEncoding];
NSLog(@"result: %@", output);
}