そのため、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_input
AppDelegateの内部のようなものもあります。これは別のクラスのものであり、それを含めても問題は解決しないためです。
お気づきかもしれませんが、私はこれをAppStoreに送信することに興味がありません。脱獄したiPhoneで使う自分用のアプリです。ありがとうございました。