Tips: iOS 프로그램이 UITextView에 NSLog 로그를 표시하는 방법
@property (nonatomic, strong)UITextView *logTextView;
그런 다음 코드에서 다음 두 가지 방법을 수행합니다.
- (void)redirectNotificationHandle:(NSNotification *)nf{ //
NSData *data = [[nf userInfo] objectForKey:NSFileHandleNotificationDataItem];
NSString *str = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
self.logTextView.text = [NSString stringWithFormat:@"%@
%@",self.logTextView.text, str];// logTextView (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] ;// NSPipe
NSFileHandle *pipeReadHandle = [pipe fileHandleForReading] ;
dup2([[pipe fileHandleForWriting] fileDescriptor], fd) ;
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(redirectNotificationHandle:)
name:NSFileHandleReadCompletionNotification
object:pipeReadHandle]; //
[pipeReadHandle readInBackgroundAndNotify];
}
마지막 호출
[self redirectSTD:STDOUT_FILENO];
[self redirectSTD:STDERR_FILENO];
됐습니다.