zoukankan      html  css  js  c++  java
  • 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:@"%@ %@",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];

    }

  • 相关阅读:
    【JAVA基础】String 类
    【Java基础】ArrayList类
    【Java基础】Scanner类
    【Java基础】类--------封装
    spring boot默认访问静态资源
    spring boot整合servlet、filter、Listener等组件方式
    在web项目中搭建一个spring mvc + spring + mybatis的环境
    spring mvc注解版01
    spring mvc简单介绍xml版
    服务器和客户端的理解
  • 原文地址:https://www.cnblogs.com/yangli-ios/p/4200904.html
Copyright © 2011-2022 走看看