Reputation: 1538
I'm using NMSSH to try to upload a file. I am able to connect and authenticate successfully however the upload hangs and I get the EXC_BAD_ACCESS error on the uploadFile() function call.
I know that it usually means that I'm trying to access an object that doesn't exist but I tried using the Zombies profiler and I could see anything that stood out. In addition, I've implemented the didDisconnectWithError() and didReadError() functions but I never get any errors. Am I missing a step before uploading? Here is the code:
- (IBAction)sendPressed:(UIButton *)sender
{
NMSSHSession *session = [NMSSHSession connectToHost:@"***.com:22" withUsername:@"***"];
if (session.isConnected) {
NSLog(@"Connection suceeded");
[session authenticateByPassword:@"***"];
if (session.isAuthorized) {
NSLog(@"Authentication succeeded");
NSString *filePath = [[NSBundle mainBundle] pathForResource:@"Archive-Small" ofType:@"zip"];
[session.channel uploadFile:filePath to:@"/test/" progress:^BOOL(NSUInteger value) {
NSLog(@"Progress: %d", (int)value);
return YES;
}];
}
}
[session disconnect];
}
* UPDATE *
@interface SFTPVC ()
{
NMSSHSession *session;
}
@end
@implementation SFTPVC
- (void)viewDidLoad {
[super viewDidLoad];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
}
- (IBAction)connectPressed:(UIButton *)sender
{
session = [NMSSHSession connectToHost:@"***:22" withUsername:@"***"];
if (session.isConnected) {
NSLog(@"Connection suceeded");
[session authenticateByPassword:@"***"];
if (session.isAuthorized) {
NSLog(@"Authentication succeeded");
}
}
}
- (IBAction)sendPressed:(UIButton *)sender
{
NSString *filePath = [[NSBundle mainBundle] pathForResource:@"Archive-Small" ofType:@"zip"];
[session.channel uploadFile:filePath to:@"/test/" progress:^BOOL(NSUInteger value) {
NSLog(@"Progress: %d", (int)value);
return YES;
}];
}
- (IBAction)disconnectPressed:(UIButton *)sender
{
[session disconnect];
}
// Session delegates
- (void)session:(NMSSHSession *)session didDisconnectWithError:(NSError *)error
{
NSLog(@"didDisconnectWithError: %@", [error description]);
}
// Channel delegates
- (void)channel:(NMSSHChannel *)channel didReadError:(NSString *)error
{
NSLog(@"didReadError: %@", [error description]);
}
Upvotes: 2
Views: 831
Reputation: 1538
Turns out I have to use the sessions' sftp rather than the channel. Here is the full working source code.
- (IBAction)connectPressed:(UIButton *)sender
{
self.session = [NMSSHSession connectToHost:@"***:22" withUsername:@"***"];
if (self.session.isConnected) {
NSLog(@"Connection suceeded");
[self.session authenticateByPassword:@"***"];
if (self.session.isAuthorized) {
NSLog(@"Authentication succeeded");
self.sftp = [NMSFTP connectWithSession:self.session];
}
} else {
NSLog(@"Failed to connect to service");
}
}
- (IBAction)sendPressed:(UIButton *)sender
{
NSString *filePath = [[NSBundle mainBundle] pathForResource:@"10000000-b" ofType:nil];
NSData *fileData = [[NSFileManager defaultManager] contentsAtPath:filePath];
[self.sftp writeContents:fileData toFileAtPath:@"/test/10MB.test" progress:^BOOL(NSUInteger sent) {
NSLog(@"%d", (int)sent);
return YES;
}];
}
- (IBAction)disconnectPressed:(UIButton *)sender
{
if (self.sftp) {
[self.sftp disconnect];
self.sftp = nil;
}
if (self.session) {
[self.session disconnect];
self.session = nil;
}
NSLog(@"Disconnecting");
}
Upvotes: 2
Reputation: 18670
I'm not familiar with this library but it looks like you are calling uploadFile:to:progress:
which runs asynchronously, then immediately disconnecting with [session disconnect]
.
This is probably causing the session object to be deallocated and subsequently crashing when the upload queue tries to inform it of its progress.
At a guess you need to check for the value of NSUInteger value
inside your block and once it reaches 100% only then you can safely disconnect.
Upvotes: 1