Monday, April 29, 2013

NSFileHandle NSFileHandleReadToEndOfFileCompletionNotification example ios


NSFileHandleReadToEndOfFileCompletionNotification


This notification is posted when the file handle reads all data in the file or, if a communications channel, until the other process signals the end of data. It makes the data available to observers by putting it in the userInfo dictionary. [NSFileHandle NSFileHandleReadToEndOfFileCompletionNotification]
To cause the posting of this notification, you must send either readToEndOfFileInBackgroundAndNotify or readToEndOfFileInBackgroundAndNotifyForModes: to an appropriate NSFileHandleobject.
The notification object is the NSFileHandle object that sent the notification. The userInfodictionary contains the following information:
Key
Value
NSFileHandleNotificationDataItemAn NSData object containing the available data read from a socket connection
@"NSFileHandleError"An NSNumber object containing an integer representing the UNIX-type error which occurred
Example of [NSFileHandle NSFileHandleReadToEndOfFileCompletionNotification]

- (void)launch {
    NSTask *task = [[[NSTask alloc] init] autorelease];
    [task setLaunchPath:@"/path/to/command"];
    [task setArguments:[NSArray arrayWithObjects:..., nil]];
    NSPipe *outputPipe = [NSPipe pipe];
    [task setStandardOutput:outputPipe];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(readCompleted:) name:NSFileHandleReadToEndOfFileCompletionNotification object:[outputPipe fileHandleForReading]];
    [[outputPipe fileHandleForReading] readToEndOfFileInBackgroundAndNotify];
    [task launch];
}
// NSFileHandleReadToEndOfFileCompletionNotification handler.
- (void)readCompleted:(NSNotification *)notification {
    NSLog(@"Read data: %@", [[notification userInfo] objectForKey:NSFileHandleNotificationDataItem]);
    [[NSNotificationCenter defaultCenter] removeObserver:self name:NSFileHandleReadToEndOfFileCompletionNotification object:[notification object]];
}