如何使用NMSSH库中的iOS委托和回调方法? [英] How to use iOS delegate and callback methods from NMSSH library?

查看:53
本文介绍了如何使用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.

这篇关于如何使用NMSSH库中的iOS委托和回调方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆