[iOS] Is it possible to use NSUrlSession to upload files to an FTP server?

Hi everyone, I'm looking for a solution to a problem that I've been having recently. Most of our customers in the field are required to upload pictures of the work that they do so as to provide legal cover in case anything bad happens. I want to use an NSUrlSession in conjunction with Background Transfer in order to opportunistically upload these pictures in the background, so as to not get in the way of the user experience. Currently, the user is unable to use the app or progress on their work while pictures upload, which is simply bad user experience. This is why I'm attempting to switch to NSUrlSession.


When looking at the NSUrlSession documentation, it does say that the class "...natively supports...ftp...URL schemes." I haven't had much luck achieving my desired goals, though. This is where it might get tricky for readers of this board, but I'll try and describe the best I can. I'm using Xamarin.iOS, but still using the NSUrlSession class and associated objects. I create an NSUrlSession object with the BackgroundSessionConfiguration and a unique identifier. I then use my NSUrlSession object with the method CreateUploadTask. My request is an NSUrl looking like so: "ftp://myftpUser:myftpPassword@myftpIP:Port/Pictures/Database/2017/10/31" and my file URL looks like so: "file:///var/mobile/Containers/Data/Application/05562862-FAC1-4C09-8477-233498882F32/Documents/PolarisPictures2/VUPSXOUTA722002799CMC5022017103109544681088_1.jpeg"


It is my understanding that using CreateUploadTask with the NSUrlSession task kicks off said task; however, nothing ever happens. So, my fundamental question is: can we upload files to an FTP server with NSUrlSession? And if so, what am I doing incorrectly here? I have no problems posting my C# code, I just figured it would be easier to say what I'm doing instead of possibly confusing readers.


Thanks,


TY

To address your specific concerns:

  • NSURLSession
    supports FTP downloads but not uploads.
  • NSURLSession
    background sessions only support HTTP[S].
  • You can do FTP uploads using

    CFFTPStream
    but:
    • That API has been deprecated for a while now (more on this below).

    • It does not support anything like the

      NSURLSession
      background transfer mechanism you want.

    The SimpleFTPSample sample code shows how to use

    CFFTPStream
    .

Looking at the bigger picture, you wrote:

Most of our customers in the field are required to upload pictures of the work that they do so as to provide legal cover in case anything bad happens.

Using FTP for this is a really bad idea. The main problem is that FTP provides no security — the data and the user’s credentials (!) travel in plain text — so you can’t draw any useful conclusions from the result.

The best way to solve this problem is to replace your FTP server with an HTTPS server. That offers good security and allows you to meet your other goal, that is, to transfer files in the background.

As I’ve said many times here on DevForums, FTP has no place on the modern Internet. And that’s the main reason that

CFFTPStream
has been deprecated: it’s not just that the API has problems, it’s that the underlying protocol is fundamentally broken.

Share and Enjoy

Quinn “The Eskimo!”
Apple Developer Relations, Developer Technical Support, Core OS/Hardware

let myEmail = "eskimo" + "1" + "@apple.com"
[iOS] Is it possible to use NSUrlSession to upload files to an FTP server?
 
 
Q