0

そのため、NSTaskをプログラムから非同期で読み取ることができましたが、ストーリーボードのUIViewのクラス内でそれを実行しました。(Obj-Cの専門家ではありません)

私のアイデアは次のとおりです。プログラムからテキストを読み取り、UITextViewに配置し、さらに多くの場合、次の方法でプロセスを繰り返します。NSNotificationCenter

これまでのところ、これは私のコードです:

LView.m:

- (void)viewDidLoad
{

    [super viewDidLoad];

    NSPipe *out_pipe = [NSPipe pipe];
    sshoutput = [out_pipe fileHandleForReading];
    [sshoutput readInBackgroundAndNotify];

    utilT = [[NSTask alloc] init];
    [utilT setLaunchPath:@"/usr/bin/utilfc9"];
    [utilT setArguments:[NSArray arrayWithObjects: @"-p", @"-f", @"log.txt", nil]];

    [utilT setStandardOutput: out_pipe];
    [utilT setStandardError: out_pipe];
    [utilT launch];

    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(readPipe:) name:NSFileHandleReadCompletionNotification object:nil];
}

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

    if( [notification object] != sshoutput ) { return };

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

    self.log.text = [self.wifilog.text stringByAppendingFormat: @"\n%@", new_input];

    if( utilT ) {
        [sshoutput readInBackgroundAndNotify];
    }
}

LView.h:

#import <UIKit/UIKit.h>
#import "NSTask.h"

NSTask *sshT;
NSFileHandle *sshoutput;

これまでのところうまく機能しているので、問題なくデータをライブで入手できます。

しかし、これNSTaskをAppDelegateのようなより「グローバルな」場所に配置application didFinishLaunchingWithOptionsしてから、データを処理して別のクラスの複数のビューを更新するにはどうすればよいでしょうか。私は試してみましたが、もちろんlog.text = new_inputAppDelegateの内部のようなものもあります。これは別のクラスのものであり、それを含めても問題は解決しないためです。

お気づきかもしれませんが、私はこれをAppStoreに送信することに興味がありません。脱獄したiPhoneで使う自分用のアプリです。ありがとうございました。

4

2 に答える 2

1

それを行う簡単な方法は

この同じ通知を受け取りたいすべてのビューに、以下を追加します

ReceiverView

-(void) viewDidLoad
{
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(read:)     name:@"ReadTest" object:nil];
}

//read function
-(void) read:(NSNotification*)notification
{ // Do something with the notification }

現在LView.mにあります

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

    if( [notification object] != sshoutput ) { return };

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

    self.log.text = [self.wifilog.text stringByAppendingFormat: @"\n%@", new_input];

    if( utilT ) {
        [sshoutput readInBackgroundAndNotify];
    }
    //Add the following
    [[NSNotificationCenter defaultCenter] postNotificationName:@"ReadTest" object:notification]
}
于 2012-06-02T19:31:47.733 に答える
0

注意してください、new_inputは割り当てられますが、解放されません=>メモリリーク

于 2012-06-02T19:36:16.800 に答える