Recently developed the program, need to do a demo to the tester, the client can display the program's print log function in real time,
Find a lot of data to find a way to use nspipe that can be achieved,
Apple's official explanation:
objects provide an object-oriented interface for accessing pipes. An NSPipe object represents both ends of a pipe and enables communication through the pipe. A pipe is a one-way communications channel between related processes; One process writes data, while the other process reads that data. The data that passes through the pipe is buffered; The size of the buffer is determined by the underlying operating system. NSPipe is an abstract class, the public interface of a class cluster.
Nspipe is an abstract class, a class of public interfaces for a cluster. Through nspipe we can easily read and write process data,
If you want to print the real-time log of the program, you can add the bottom method to the controller
-(void) Redirectnotificationhandle: (nsnotification *) nf{//notification method NSData *data = [[NF UserInfo] Objectforkey:nsfilehandl Enotificationdataitem]; NSString *str = [[NSString alloc] Initwithdata:data encoding:nsutf8stringencoding]; Self.logTextView.text = [NSString stringwithformat:@ "%@\n\n%@", Self.logTextView.text, str];//Logtextview is to have the log output view (Uitextview) Nsrange range; range.location = [Self.logTextView.text length]-1; range.length = 0; [Self.logtextview Scrollrangetovisible:range]; [[NF Object] readinbackgroundandnotify];} -(void) REDIRECTSTD: (int) fd{nspipe * pipe = [nspipe pipe];//Initialize a Nspipe object nsfilehandle *pipereadhandle = [pipe Filehandleforreading]; DUP2 ([[Pipe filehandleforwriting] filedescriptor], FD); [[Nsnotificationcenter Defaultcenter] addobserver:self selector: @selector (Redi Rectnotificationhandle:) name:nsfilehandlereadcompletionnOtification Object:pipereadhandle]; Registration notice [Pipereadhandle readinbackgroundandnotify];}
Perform
[self redirectstd:stdout_fileno];
[self redirectstd:stderr_fileno];
Output printing can be achieved,
The IOS program displays the NSLog log method in the Uitextview,