我试图在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");
}
连接服务器,从Console中的服务器发回单行命令和确认即可。
我非常了解如何使用委托将值从一个View Controller传递到另一个View Controller(通过一些实际实现的教程)。
凭借相同的知识,我试图从NMSSH库的委托方法部分得到回应,但它驱使我四处走动。我发现这个库的http://cocoadocs.org/docsets/NMSSH/2.2.1/非常好的API,但由于我对iOS的了解有限,我有点卡住了。
请帮帮我。
答案 0 :(得分:0)
我的搜索终于结束了支持多线程的NMSSH AsyncAPI(分支)。