1

Ok so I'm trying to make an app that is basically a GUI for a script the part I can't get to work correctly is displaying the output of the script at first I had it so it should me everything after task had completed which needless to say is no good. Where I'm at now is I can get the output in real time in a uitextview but as new information is being delivered it over laps making it unreadable I use the script apt-get update in my example. I am a new jailbreak dev and yes I have my app running with root permission my only problem is the output... My code is this:

#import "RootViewController.h"

@implementation RootViewController
@synthesize navItem;
- (void)loadView {
    self.view = [[[UIView alloc] initWithFrame:       [[UIScreen mainScreen] applicationFrame]]        autorelease];
    self.view.backgroundColor = [UIColor        redColor];

    navBar = [[UINavigationBar alloc] init];
    navBar.frame = CGRectMake(0, 0,         self.view.frame.size.width, 44);

    navItem = [[[UINavigationItem alloc]
    initWithTitle:@"GUI"] autorelease];
    navBar.barStyle = UIBarStyleDefault;
    navBar.items = [NSArray     arrayWithObject:navItem];
    [self.view addSubview:navBar];

    NSPipe *pipe = [NSPipe pipe];

    _fileHandle = [pipe fileHandleForReading];
    [_fileHandle readInBackgroundAndNotify];

    task = [[NSTask alloc] init];
    [task setLaunchPath:@"/usr/bin/apt-get"];
    [task setStandardOutput: pipe];
    [task setStandardError: pipe];

        NSArray *arguments;
    arguments = [NSArray arrayWithObjects:     @"update", nil];
    [task setArguments: arguments];

    [task launch];

}
-(id)init
{
    [super init];
    [[NSNotificationCenter defaultCenter]     addObserver:self
    selector:@selector( readPipe: )
      name:NSFileHandleReadCompletionNotification 
    object:nil];
    return self;
}

-(void)dealloc
{
    [[NSNotificationCenter defaultCenter]     removeObserver:self];
}

-(void)readPipe: (NSNotification *)notification
{
    NSData *data;
    NSString *text;

    if( [notification object] != _fileHandle )
        return;

    data = [[notification userInfo] objectForKey:NSFileHandleNotificationDataItem];
    text = [[NSString alloc] initWithData:data encoding:NSASCIIStringEncoding];


    navItem.title = text;

    titleTextField = [[UITextView alloc]   initWithFrame: CGRectMake(0, 40, 320, 350)];
    [titleTextField setBackgroundColor:[UIColor     clearColor]];
    titleTextField.text = text;
    titleTextField.editable = YES;
    titleTextField.scrollEnabled = YES;
    titleTextField.autoresizingMask =UIViewAutoresizingFlexibleHeight;
    [self.view addSubview: titleTextField];

    [text release];
    if( task )
    [_fileHandle readInBackgroundAndNotify];

}

@end
Ramy Al Zuhouri
  • 21,580
  • 26
  • 105
  • 187
  • Put a breakpoint in the line: "data = [[notification userInfo] objectForKey:NSFileHandleNotificationDataItem];" . Does the debugger even step there? – Ramy Al Zuhouri Jan 12 '13 at 20:23

1 Answers1

0

Each time readPipe is called with new output from the task, you create a new UITextView with the same frame rectangle. This is the reason that the new text overlaps the previously shown text.

You should either use a single UITextView and always append the new text in readPipe, or create each text view a different frame.

Martin R
  • 529,903
  • 94
  • 1,240
  • 1,382
  • And you should be mucking w/the UITextView on the main thread only. – bbum Jan 12 '13 at 23:39
  • 1
    @bbum: The documentation for `readInBackgroundAndNotify` states: "... and posts an NSFileHandleReadCompletionNotification notification on the current thread ..." - If "current thread" means the thread from which `readInBackgroundAndNotify` was started, then the notification should be on the main thread again. – Martin R Jan 12 '13 at 23:46