【问题标题】:How to use iOS delegate and callback methods from NMSSH library?如何使用 NMSSH 库中的 iOS 委托和回调方法?
【发布时间】:2015-07-17 14:05:38
【问题描述】:

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

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");
}

连接到服务器,在控制台中从服务器发送单行命令和确认就可以了。

我很清楚如何使用委托将值从一个视图控制器传递到另一个视图控制器(通过一些实际实现的教程)。

以同样的知识,我试图从 NMSSH 库的委托方法部分获得响应,但它让我不断地转来转去。我发现 http://cocoadocs.org/docsets/NMSSH/2.2.1/ 这个库的 API 非常好,但由于我对 iOS 的了解有限,我有点卡住了。

请帮帮我。

【问题讨论】:

  • 您是否确定 if (session.isAuthorized) { 返回 true 并且条件中的行正在执行?由于您在 if 中设置了委托,因此请确保它们正在执行。
  • [session.channel execute:@"command" error:&error]; NSLog(@"来自设备的响应:%@", response);我可以在服务器上确认命令正在执行,并且在 iOS 控制台中确认服务器返回的响应。因此 session.authorized 没有问题。我还在 session.authorized 之外尝试了更多组合设置委托。

标签: ios delegates nmssh


【解决方案1】:

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

【讨论】:

  • 我正在尝试使用 NMSSH AsyncAPI,但我无法连接或上传文件到 SFTP。对你有用吗?我尝试使用可可豆荚并手动添加构建。
  • 它确实对我有用。我记得我从 github 构建(下载项目),编译(尽管有错误)并将 NMSSH AsyncAPI 包拉(拖放)到我的新项目中。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-07-16
  • 1970-01-01
  • 1970-01-01
  • 2018-10-07
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多