如何使用NMSSH库中的iOS委托和回调方法?

问题描述:

我正在尝试使用iOS中NMSSH库中的委托方法,但无法正常工作.让我们举个例子.

I'm trying to use delegate methods from NMSSH library in iOS but could not get it working. Let's take an example.

CustomViewController.h

#import <UIKit/UIKit.h>
#import <NMSSH/NMSSH.h>

@interface CustomViewController : UIViewController<NMSSHSessionDelegate, NMSSHChannelDelegate>

- (IBAction)connectButton:(UIButton *)sender;

@end

CustomViewController.m

#import "CustomViewController.h"

@implementation CustomViewController

-(void)viewDidLoad{
    [super viewDidLoad];
}

- (IBAction)connectButton:(UIButton *)sender {

        [self serverConnect:@"10.0.0.1"];

}

-(void)serverConnect:(NSString *)address{

NMSSHSession *session = [NMSSHSession connectToHost:address withUsername:@"username"];

NMSSHChannel *myChannel = [[NMSSHChannel alloc]init];

    if (session.isConnected) {
        [session authenticateByPassword:@"password"];

        if (session.isAuthorized) {
            NSLog(@"Authentication succeeded");
            [session setDelegate:self];
        [myChannel setDelegate:self];
        }
    }

        NSError *error = nil;
        //session.channel.requestPty = YES; (tried and later ignored)
        NSString *response = [session.channel execute:@"mkdir" error:&error];
        NSLog(@"Response from device: %@", response);
}

- (void)session:(NMSSHSession *)session didDisconnectWithError:(NSError *)error{
    NSLog(@"log if session disconnects...Delegate method");
}

- (void)channel:(NMSSHChannel *)channel didReadError:(NSString *)error{
    NSLog(@"Error received...Delegate method");
}

- (void)channel:(NMSSHChannel *)channel didReadRawData:(NSData *)data{
    NSLog(@"Read Raw Data...Delegate method");
}

连接到服务器,发送单行命令,并在控制台中从服务器返回确认.

Connection to the server, sending a single line command and acknowledgement back from the server in Console is OK.

我有一个不错的主意,如何使用委托将值从一个View Controller传递给另一个View Controller(通过了一些具有实际实现的教程).

I have decent idea how to pass values from one View Controller to another using delegate (went through few tutorials with practical implementation).

以同样的知识,我试图从NMSSH库的委托方法部分获得响应,但它使我无处不在.我发现 http://cocoadocs.org/docsets/NMSSH/2.2.1/一个>这个库相当不错的API,但是由于我对iOS的了解有限,我有点卡住了.

With the same knowledge I am attempting to get response from delegate methods parts of NMSSH library but it's driving me round and round. I've found http://cocoadocs.org/docsets/NMSSH/2.2.1/ pretty nice API of this library but with my limited knowledge of iOS, I'm bit stuck.

请帮助我.

我的搜索终于以支持多线程的NMSSH AsyncAPI(分支)结束了.

My search finally came to an end with NMSSH AsyncAPI (branch) which supports multithreading.