好的,所以我正在尝试制作一个基本上是脚本的 GUI 的应用程序,我无法正常工作的部分是显示脚本的输出,起初我有它,所以它应该是我完成任务后的所有内容不用说是没有好处的。我现在所处的位置是我可以在 uitextview 中实时获取输出,但是随着新信息的传递,它会经过数圈传递,使其不可读,我在示例中使用了脚本 apt-get update。我是一个新的越狱开发者,是的,我的应用程序以 root 权限运行我唯一的问题是输出......我的代码是这样的:
#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