如何使用 NMSSH 库中的 iOS 委托和回调方法?
How to use iOS delegate and callback methods from NMSSH library?
我正在尝试使用 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");
}
连接到服务器,发送单行命令并在控制台中从服务器返回确认是正常的。
我很清楚如何使用委托将值从一个 View Controller 传递到另一个 View Controller(通过一些教程进行了实际实施)。
基于同样的知识,我试图从 NMSSH 库的委托方法部分获得响应,但它让我四处奔波。我发现这个库 http://cocoadocs.org/docsets/NMSSH/2.2.1/ 非常好 API,但由于我对 iOS 的了解有限,我有点卡住了。
请帮助我。
我的搜索终于以支持多线程的 NMSSH AsyncAPI(分支)结束了。
我正在尝试使用 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");
}
连接到服务器,发送单行命令并在控制台中从服务器返回确认是正常的。
我很清楚如何使用委托将值从一个 View Controller 传递到另一个 View Controller(通过一些教程进行了实际实施)。
基于同样的知识,我试图从 NMSSH 库的委托方法部分获得响应,但它让我四处奔波。我发现这个库 http://cocoadocs.org/docsets/NMSSH/2.2.1/ 非常好 API,但由于我对 iOS 的了解有限,我有点卡住了。
请帮助我。
我的搜索终于以支持多线程的 NMSSH AsyncAPI(分支)结束了。