iosdelegatesnmssh

How to use iOS delegate and callback methods from NMSSH library?


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.

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

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.

Please help me.


Solution

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