The following describes how to use the Third-party library Rebekka for FTP client development, the implementation of the features include: FTP server connection, file list query, folder creation, upload files, download files.
Description and configuration of 1,rebekka
Rebekka is a FTP/FTPS client action library written in Swift language. Its internal encapsulation uses the Cfftpstream API of the Cfnetworking network library.
Download Address: Https://github.com/Constantine-Fry/rebekka
(1) Drag "rebekka.xcodeproj" into the project
(2) Add "Rebekkatouch" in the "project"-> "Build Phases"-> "Target dependencies".
(3) When you need to use the import can be
(3) When you need to use the import can be
Import Rebekkatouch
2, initialize the connection configuration
Import Uikit
Import Rebekkatouch
Class Viewcontroller:uiviewcontroller {
var session:session!
Override Func Viewdidload () {
Super.viewdidload ()
var configuration = sessionconfiguration ()
Configuration.host = "Ftp://ftp.hangge.com"
Configuration.username = "Ftpuser"
Configuration.password = "123456"
Session = Session (Configuration:configuration)
}
Override Func didreceivememorywarning () {
Super.didreceivememorywarning ()
}
}
3, get the file, folder List and output
Below the root directory of all the files, folder names printed (of course, can also get other information, such as the file creator, creation time, etc.)
Showlist ("/")
Func showlist (path:string) {
Session.list (path) {
(Resources, error)-> Void in
For item in resources! {
Print ("File type: \ (item.type) file name: \ (item.name)")
}
}
}
4, create a new folder
CreateDirectory ("/upload/Test Folder")
Func CreateDirectory (path:string) {
Session.createdirectory (path) {
(result, error)-> Void in
If result {
Print ("Folder creation succeeded!")
}else {
Print ("folder creation failed: \ (Error)")
}
}
}
5, Upload file
Func Testuploadfile () {
Local files
Let URL = Nsbundle.mainbundle (). Urlforresource ("logo", withextension: "PNG")
Server path and saved file name
Let path = "/upload/\" (Nsuuid (). uuidstring). png "
Uploading files
Session.upload (url!, Path:path) {
(result, error)-> Void in
If result {
Print ("File upload successful!")
}else {
Print ("File upload failed: \ (Error)")
}
}
}
6, download the file
The files downloaded by default are saved in a temporary folder and named randomly. We can rename it by moving it to the desired directory (such as the user's document directory) in the downloaded callback method.
Func Testdownloadfile () {
Self.session.download ("/upload/hangge.png") {
(FileURL, error)-> Void in
Print ("File Save Address: \ (FileURL)")
Download the temporary files to be handled by yourself
/**
If let FileURL = FileURL {
do {
Try Nsfilemanager.defaultmanager (). Removeitematurl (FileURL)//delete temporary files
Catch let error as nserror {
Print ("Error: \ (Error)")
}
}**/
}
}