3

First of all: I know about the security concerns regarding ftp, so please no discussion about this here - ftp has to be used.

My app has to log into a server with ftp (with username/password), search for and download a file, delete it from the server, then upload an edited version.

I've been searching for tutorials on how to even get a connection to a server via ftp but all I found were Apple's documentation from 2012 here and their documentation about the different functions here (and a bunch of weird "Swift FTP" biking videos on youtube...). Every other example I found (usually <2015) uses some additional framework or library but there don't seem to be any proper tutorial for Swift 5 or even Swift 4.

Is it even still possible to use FTP with Swift 5 (is it still supported?) and if so, how? Or is there simply no point trying to get any of Apple's stuff to work and better to just use an extra framework like FileProvider?

rmaddy
  • 314,917
  • 42
  • 532
  • 579
Neph
  • 1,823
  • 2
  • 31
  • 69
  • 1
    The documentation from 2012 is still valid. Most of the API is deprecated but it still works. It's possible to write a Swift 5 version without any additional frameworks. You need a `socket` related operation to send the raw FTP commands and a `stream` related operation to transfer data. – vadian May 09 '19 at 10:28
  • Thanks for your answer! Are any of the important parts deprecated too or is everything (logging in, downloading, searching, uploading) still possible and working? Slightly off-topic: From your experience, is it still worth getting into it or would you recommend starting with a custom framework/library? – Neph May 09 '19 at 11:02
  • The entire `CFFTP...` API is deprecated. It's used for example to read the contents of a directory. The [`Stream`](https://developer.apple.com/documentation/foundation/stream) and BSD socket API is not deprecated. The subject is not trivial, but the Apple documentation (although in ObjC) is a good starting point. – vadian May 09 '19 at 11:10
  • I see. Is there an alternative for searching a directory? I'm currently trying to get into it but I don't even know how to start and my non-existing knowledge of ObjC doesn't help. I simply can't find any good information about Swift - do you happen to know a decent tutorial? – Neph May 09 '19 at 11:20
  • I don't think there are any tutorials. For my purpose I translated the ObjC code into Swift and the C API of BSD socket can be used in Swift with only a few adjustments. – vadian May 09 '19 at 12:37
  • Oh, now I understand why others would rather use the extra library instead of translating stuff between ObjC and Swift. Thanks for you answers! – Neph May 09 '19 at 13:23
  • Just a quick update: I'm currently using [FileProvider](https://github.com/amosavian/FileProvider) - it's the only library I found that wasn't last updated 1+ years ago. It still works but unfortunately it's a bit buggy: Every function I've tested so far (downloading/reading files on my ftp server, see my other [question](https://stackoverflow.com/q/56113593/2016165)) is being called twice and it doesn't look like the author is still working on the library or fixing bugs. – Neph May 20 '19 at 11:07
  • I don't know the library, sorry. – vadian May 20 '19 at 11:31
  • The comment was simply meant as an update for everyone who should come across this question. ;) – Neph May 20 '19 at 11:38
  • @Neph Is there any news? I was looking for FTP issues without framework with Swift 5.0. One of my friend used 'rebekka' pod for FTP. It works. But as I wrote above I am looking for FTP issues without frameworks with Swift 5.0 – mannyCalavera Feb 25 '20 at 08:27
  • @mannyCalavera I also looked at `rebekka` last year but didn't use it because it's for Swift 2 - I'm surprised it still works. And no, no news, as I haven't worked on the app lately. Plus, I'm using the socket library as stated below, which is working perfectly fine and unless a new Swift version completely breaks that library, I don't think I'm going to change it again. – Neph Feb 25 '20 at 09:45
  • @Neph thanks. Is it possible you to share this socket library with me? – mannyCalavera Feb 26 '20 at 05:10
  • @mannyCalavera You can find the link in the answer I posted. – Neph Feb 26 '20 at 09:51
  • BlueSocket, okay. – mannyCalavera Feb 26 '20 at 12:33

1 Answers1

1

At first I was using FileProvider, unfortunately the library is pretty buggy currently, with functions being called twice (which you can kind of prevent by using a "firstTimeCalled" bool check) and if the server's slow(-ish), you also might not get e.g. the full list of files in a directory because FileProvider stops receiving answers before the server's actually done.

I haven't found any other FTP libraries for Swift that work (and are still supported), so now I'm using BlueSocket (which is able to open sockets, send commands to the server and receive commands from it) and built my own small library that can send/receive,... files (using the FTP codes) around it.

Neph
  • 1,823
  • 2
  • 31
  • 69