IOS 把shell信息或者NSlog输出信息 显示在视图
//标准输出
[self redirectSTD:STDOUT_FILENO];
//错误输出
//[self redirectSTD:STDERR_FILENO];
- (void)redirectNotificationHandle:(NSNotification *)nf{
NSData *data = [[nf userInfo] objectForKey:NSFileHandleNotificationDataItem];
NSString *str = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
if (![str isEqualToString:@""]) {
textView.text = [NSString stringWithFormat:@"%@\n%@",textView.text, str];
NSRange range;
range.location = [textView.text length] - 1;
range.length = 0;
[textView scrollRangeToVisible:range];
}
[[nf object] readInBackgroundAndNotify];
}
- (void)redirectSTD:(int )fd{
NSPipe * pipe = [NSPipe pipe] ;
NSFileHandle *pipeReadHandle = [pipe fileHandleForReading] ;
dup2([[pipe fileHandleForWriting] fileDescriptor], fd) ;
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(redirectNotificationHandle:)
name:NSFileHandleReadCompletionNotification
object:pipeReadHandle] ;
[pipeReadHandle readInBackgroundAndNotify];
}
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。