首页 > 移动平台 > 详细

IOS 把shell信息或者NSlog输出信息 显示在视图

时间:2015-01-04 14:46:42      阅读:366      评论:0      收藏:0      [点我收藏+]

//标准输出

[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];

}

 

IOS 把shell信息或者NSlog输出信息 显示在视图

原文:http://www.cnblogs.com/yangli-ios/p/4200904.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!