1

I am trying to use the SFTP library in NMSSH to download files from an ftp server and have been having trouble on my actual device. It works fine in the simulator. When I check for the file's existence in the stored directory (a created folder in my app's cache directory), the check passes on the simulator but fails on the phone. Below is the code I'm using. As a note, MainPath is the directory path and the fileListLevel1 row item is the file name with the extension.

        NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
        NSString *cacheDirectory = paths[0];
        cacheDirectory = [@[cacheDirectory, @"/NewDirectory", @"/"] componentsJoinedByString:@""];
        [[NSFileManager defaultManager] createDirectoryAtPath:cacheDirectory withIntermediateDirectories:false attributes:nil error: nil];
        NSString *filePathToOpen = [@[MainPath, [fileListLevel1 objectAtIndex:indexPath.row]] componentsJoinedByString:@""];
        bool isFile = NO;
        bool isDir = NO;
        isFile = [session.sftp fileExistsAtPath:filePathToOpen];
        isDir = [session.sftp directoryExistsAtPath:filePathToOpen];
        if (isFile){
            NSString *fileToOpenNameAndPath = [@[cacheDirectory, [fileListLevel1 objectAtIndex:indexPath.row]] componentsJoinedByString:@""];
            [session.sftp writeFileAtPath:filePathToOpen toFileAtPath:fileToOpenNameAndPath];
                //FILE NOT FOUND ON THE PHONE - FOUND IN SIMULATOR!!!!!!!!
            if ([[NSFileManager defaultManager] fileExistsAtPath:fileToOpenNameAndPath]){
                NSLog(@"Found you!");
            }
            else if(![[NSFileManager defaultManager] fileExistsAtPath:fileToOpenNameAndPath]){
                NSLog(@"Why you no there?");
            }

I also tried creating the paths using the stringByAppendingPathComponent method and got the same result.

I'm new to app writing and objective c so I may be missing something fundamental here but I've been unable to find anything on this.

One additional question revolves around using the writeFileAtPath:toFileAtPath:progress command. I wonder if my issue is with the simulator writing much faster so my check happens before it is done on the phone. I'm not sure how to properly use the :progress portion as I'm unsure what type of variable the (BOOL (^)(NSUInteger sent)) is referring to or how to create one to check the progress.

Any help would be very much appreciated.

Nuno Cruces
  • 1,584
  • 15
  • 18
B. Jewell
  • 11
  • 2
  • I notice that I get the following warning when I run this on my phone: NMSSH: No bytes available in the stream This indicates that the NSInput stream "hasBytesAvailable" check is failing for some reason. I'm not sure what the phone is doing different from the simulator that would cause this. – B. Jewell Feb 29 '16 at 21:37
  • Found the issue. It seems NMSSH 'writeFileAtPath' couldn't access the app's cache directory on the iPhone. Changing code to save 'contentsAtPath' to an 'NSData' variable and then saved that variable to the file using Xcode's built in 'writeToFile'. Not sure why I couldn't use NMSSH's sftp write commands but this seems to work on both the phone and the simulator. – B. Jewell Mar 01 '16 at 21:02

0 Answers0