The IOS program displays the NSLog log method in the Uitextview,

Source: Internet
Author: User

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,

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.