0

I'm using the following code to get a directory listing for an ftp app. It works as expected in iOS, but in macOS, I get no results - data.length is always 0 - and no errors. I've also tried using Session and Task delegates, but get the same results.

Any suggestions?

The format of the ftpURL is "ftp://server.host.net/Public"

Note that I can use the CFFTP methods to get a directory listing, and they do work, but... they are deprecated. Also, the NSURLSession download tasks are working just fine.

- (void)directoryListing:(NSURL *)ftpURL
{
    NSLog(@"Directory listing for:\n%@", ftpURL.absoluteString);
    NSURLSessionDataTask *dataTask = [[NSURLSession sharedSession] dataTaskWithURL:ftpURL completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
        if (error != nil) {
            NSLog(@"Client-Error:%@",error.localizedDescription);
        } else {
            NSString    *results = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
            NSLog(@"Data length: %ld, [%@]", data.length, results);
        }
    }];
    [dataTask resume];
}

1 Answers1

0

I'm really surprised it works in iOS. FTP is basically ultra-deprecated at this point, and is only officially supported for file downloads. Retrieving a list of files (LIST command) uses an entirely different command from downloading a file (RETR command), and there's no real standard for how the results of the latter should be provided by the Foundation networking APIs.

My guess is that you're accidentally getting data from that call because of some quirk in the way Safari on iOS was written (e.g. Safari not using CFNetwork calls directly on iOS), combined with some subtle difference in the way the foundation networking code underneath ties into the CFNetwork layer. In other words, it's a fluke that it works.

I would probably recommend using an FTP library that's actually supported, e.g. libcurl. You'll also have the advantage of being able to support TLS, which AFAIK the Apple FTP code has never supported at all.

dgatwood
  • 10,129
  • 1
  • 28
  • 49