Swift implements FTP client production (using Rebekka library)

Source: Internet
Author: User
Tags file upload ftp ftp client

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)")
}
}**/
}
}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.