FTP Client for iOS .

Posted in: Blog

One of Appdev360 clients required an iOS application that could enable downloading and uploading files through File Transfer Protocol (FTP). Appdev360 engineer Asif Bilal performed an extensive research to find different libraries that were suitable for developing the application. These were:

  • WhiteRaccoon (Objective C)
  • BlackRaccoon (Objective C)
  • Rebekka (Swift)
  • SimpleFTPSample (Objective C, Apple)

However, Asif used Rebekka because of Swift as it is the core programming language created for iOS. He took following steps for developing the application:

  • Included Rebekka library as a submodule and imported the ‘Rebecca Touch’ framework. Declared a variable of Session (class) in one of the Singleton class as:
import RebekkaTouch

class SingletonClass:NSObject { // Singleton Class

    static let sharedInstance: SingletonClass = SingletonClass() // This is how we make a class singleton.

    var session: Session! // So that this variable should never be reallocated. Also, can be accessed via any class in the application.
}
  • Initialized the Session’s variable in the application
import UIKit
import RebekkaTouch

class AViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.
        
        var configuration = SessionConfiguration()
        configuration.host = host.text!
        configuration.username = username.text!
        if let passwordText = password.text {
            configuration.password = passwordText
        }
        SingletonClass.sharedInstance.session = Session(configuration: configuration)
    }

}
  • Utilized following methods in the Session Class to incorporate required features such as:

a- List Files

/** The FTP session. */
public class Session {
    public init(configuration: RebekkaTouch.SessionConfiguration, completionHandlerQueue: NSOperationQueue = default)
    /** Returns content of directory at path. */
    public func list(path: String, completionHandler: RebekkaTouch.ResourceResultCompletionHandler)

class BViewController: UIViewController {

    func listFiles(nodeURL:String) {

        SingletonClass.sharedInstance.session.list(nodeURL) { (resources, error) in
            
            print("List directory with result:\n\(resources), error: \(error)\n\n")
            
        }
    }

}

b- Create Directory

/** Creates new directory at path. */
    public func createDirectory(path: String, completionHandler: RebekkaTouch.BooleanResultCompletionHandler)

class BViewController: UIViewController {

    func createDirectory(nodeURL:String, directoryName:String) {

        let name = NSUUID().UUIDString
        SingletonClass.sharedInstance.session.createDirectory(“\(nodeURL)/\(directoryName)") { (result, error) in
            print("Create directory with result:\n\(result), error: \(error)")
        }
    }

}

c- Download File

 /** 
    Downloads file at path from FTP server.
    File is stored in /tmp directory. Caller is responsible for deleting this file. */
    public func download(path: String, completionHandler: RebekkaTouch.FileURLResultCompletionHandler)

class BViewController: UIViewController {

    func downloadFile(nodeURL:String) {
        
        SingletonClass.sharedInstance.session.download(nodeURL) { (fileURL, error) in
            print("Download file with result:\n\(fileURL), error: \(error)\n\n")
            if let fileURL = fileURL {
                do {
                    try NSFileManager.defaultManager().removeItemAtURL(fileURL)
                } catch let error as NSError {
                    print("Error: \(error)")
                }
            }
        }
    }

}

d- Upload File

       /** Uploads file from fileURL at path. */
    public func upload(fileURL: NSURL, path: String, completionHandler: RebekkaTouch.BooleanResultCompletionHandler)
}

class BViewController: UIViewController {

    func uploadFile(nodeURL:String) {

        if let URL = NSBundle.mainBundle().URLForResource("TestUpload", withExtension: "png") {
            let path = “\(nodeURL)/\(NSUUID().UUIDString).png"

            SingletonClass.sharedInstance.session.upload(URL, path: path) { (result, error) in
                print("Upload file with result:\n\(result), error: \(error)\n\n")
            }
        }
    }

}









Leave a Reply

Your email address will not be published. Required fields are marked *