將輸出綁定到UITextView

使用的iOS重定向的方式,將原本輸出到Xcode內的文本全部綁定到UITextView上。原來的地址不記得了。

- (void)redirectNotificationHandle:(NSNotification *)nf{  
  NSData *data = [[nf userInfo] objectForKey:NSFileHandleNotificationDataItem];  
  NSString *str = [[[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding] autorelease];  
  
  self.logTextView.text = [NSString stringWithFormat:@"%@\n%@",self.logTextView.text, str];  
  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] ;  
  NSFileHandle *pipeReadHandle = [pipe fileHandleForReading] ;  
  dup2([[pipe fileHandleForWriting] fileDescriptor], fd) ;  
  
  [[NSNotificationCenter defaultCenter] addObserver:self  
                                           selector:@selector(redirectNotificationHandle:)  
                                               name:NSFileHandleReadCompletionNotification  
                                             object:pipeReadHandle] ;  
  [pipeReadHandle readInBackgroundAndNotify];  
}  
- (BOOL)application:(UIApplication *)application   
didFinishLaunchingWithOptions:(NSDictionary *)launchOption{  
  
  [self redirectSTD:STDOUT_FILENO];  
  [self redirectSTD:STDERR_FILENO];  
  
//YOUR CODE HERE...  
} 

如果想要去掉該功能將最下方的方法屏蔽掉就好了。
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章