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.