13

I am trying to upload files via FTP to server. According to Apple Documentation NSURLSession class supports FTP operations.

There is a famous Apple Developer blog which also supports that. But still its not clear whether NSURLSession API's supports ftp upload or not? (I tried with the way suggested and getting error).

With the conventional way of CFFTPStreamRef, ftp upload works fine but it's deprecated in 9.0. The header says: CF_DEPRECATED(10_3, 10_11, 2_0, 9_0 , "Use NSURLSessionAPI for ftp requests")

Any idea, example or link to get help with. I am trying something like this for now:

NSURL *url_upload = [NSURL URLWithString:@"ftp://username:password@thelink/myfolder/filename.zip"];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:url_upload];
    [request setHTTPMethod:@"PUT"];
NSURL *docsDirURL = [NSURL fileURLWithPath:filePath];

NSURLProtectionSpace * protectionSpace = [[NSURLProtectionSpace alloc] initWithHost:url_upload.host port:[url_upload.port integerValue] protocol:url_upload.scheme realm:nil authenticationMethod:nil];

NSURLCredential *cred = [NSURLCredential
                         credentialWithUser:userId
                         password:password
                         persistence:NSURLCredentialPersistenceForSession];


NSURLCredentialStorage * cred_storage ;
[cred_storage setCredential:cred forProtectionSpace:protectionSpace];
NSURLSessionConfiguration *sessionConfig = [NSURLSessionConfiguration defaultSessionConfiguration];
sessionConfig.URLCredentialStorage = cred_storage;
sessionConfig.timeoutIntervalForRequest = 30.0;
sessionConfig.timeoutIntervalForResource = 60.0;
sessionConfig.allowsCellularAccess = YES;
sessionConfig.HTTPMaximumConnectionsPerHost = 1;
NSURLSession *upLoadSession = [NSURLSession sessionWithConfiguration:sessionConfig delegate:self delegateQueue:nil];
NSURLSessionUploadTask *uploadTask = [upLoadSession uploadTaskWithRequest:request fromFile:docsDirURL];
[uploadTask resume];
Amit
  • 1,043
  • 1
  • 10
  • 32

1 Answers1

6

To the best of my recollection, NSURLSession (and NSURLConnection) support retrieving files via FTP, but do not support any other FTP commands, such as STOR (note that PUT is an HTTP method, not an FTP method).

For your purposes, your options are to either use the CFFTPStream API (which only barely works) or stop using FTP.

My strong recommendation would be to stop using FTP. The FTP protocol is hopelessly insecure, sending the username and password in cleartext form over the wire, which makes it really easy for people to sniff the credentials and masquerade as the user. So the only situation where an FTP upload would be even remotely acceptable these days is an anonymous FTP upload to a shared dropbox, and even then, it is somewhat dubious. That's why the functionality was never added to the NSURLConnection API, much less NSURLSession.

There are much better alternatives that are much more secure, such as WebDAV over HTTPS, POST request uploads over HTTPS, WebDAV or POST requests with digest authentication, etc. And those alternatives are actually supportable with NSURLSession and provide other advantages like the ability to resume downloads. Unless you have absolutely no way to change the server side, please use one of those alternatives instead.

dgatwood
  • 10,129
  • 1
  • 28
  • 49
  • 1
    NSURLSession documentation is really confusing to make out what & how it supports on FTP. This is a nice example of not so good docs from Apple. – Amit Dec 04 '15 at 01:22
  • 1
    It is unfortunate that somebody got the bright idea of adding a deprecation warning for the Core Foundation FTP APIs, implying that you can replace them with NSURLSession. That was a serious mistake. In defense of the docs, though, I doubt anybody even considered the possibility that someone would still be trying to *upload* files with FTP. That was considered a very bad security practice *twenty years* ago—so much so that I wouldn't be surprised if Apple removed support for FTP uploads entirely in the not-too-distant future, because it simply should never be done. – dgatwood Dec 04 '15 at 08:11
  • 1
    I'm getting a bit sick of the FTP bashing that coders keep indulging in. The security issue is a red herring - use FTP over TLS!!! And there is no good reason to consign it to the dustbin at all... Most web hosts still only provide FTP facilities to upload site content, for example. Where would we be if that stopped working, eh? – Simon Tillson Dec 07 '16 at 16:45
  • The APIs in question don't support the ftps protocol. If anybody is still trying to upload files using this API, they need a serious security reality check. – dgatwood Dec 07 '16 at 19:19
  • 1
    I have active FTP server configured. But get the error as Error Domain=NSURLErrorDomain Code=-1102 "You do not have permission to access the requested resource.". How can set to active server as it was possible using kCFStreamPropertyFTPUsePassiveMode in NSInputStream API. username,password are correctly set. – mohit_IBS Dec 19 '16 at 13:23
  • I resolved the issue Error Domain=NSURLErrorDomain Code=-1102 "You do not have permission to access the requested resource.". but Now getting "Request time out." error. Is there any else need to be set for downloading file from active FTP server – mohit_IBS Dec 19 '16 at 13:38
  • If the request is timing out, that usually means you're trying to use active FTP mode from behind a NAT firewall. – dgatwood Dec 19 '16 at 14:22
  • Is there any way to set Passive mode property in NSURLSession API for FTP request? – mohit_IBS Dec 20 '16 at 09:18
  • 1
    @dgatwood but i am sure that my IP Addresses are white listed in firewall. i can download with cyberduck or filezilla in active mode but, programmatically i am unable to download. – Prabhat Kasera Dec 20 '16 at 09:22
  • No idea. You might try opening another question and see if somebody else knows what might be happening. – dgatwood Dec 20 '16 at 16:44
  • 1
    I don't think discarding FTP right away is the right approach, there can be number of reasons where one has to use FTP over http, I don't want to disclose the details, but in my current product I cannot use http, the only way out is ftp and moreover question asked was pertaining to use of ftp through nsurlsession , it does not make sense to list down disadvantages of ftp, as everyone is aware of those drawbacks. – Jas_meet Jul 04 '17 at 06:26
  • I'm not convinced everyone is aware of just how serious the drawbacks are (a fundamental lack of security), or else this question would not have even been asked. NSURLConnection has no FTP upload support, and was designed to replace the CF-level APIs (circa 2003). FTP uploads were effectively deprecated fourteen years ago. If you're still using them now, your requirements are fundamentally flawed. Any legacy hardware that can't support HTTP should have been retired a decade back, so there's absolutely no possible valid reason to still support FTP uploads—only excuses. – dgatwood Jul 04 '17 at 14:48