To work with files, create the HHFWFiles application, which used framework methods:
Method name | Brief description |
initWithCredentials (_: host: environment: project: application: device:) | The method initializes the framework. |
auth (_: password:) | The method authenticates the user by login and password. |
getDirectory (_: mountName: handler:) | The method returns information about directory contents. |
getFileMeta (_: mountName: handler:) | The method returns information about file. |
getFile (_: mountName: handler:) | The method downloads file from mobile platform server. |
putFile (_: mountName: handler:) | The method uploads file to mobile platform server. |
deleteFile (_: mountName: handler:) | The method deletes file from mobile platform server. |
The HHFWFiles application includes one screen, the UITextField text box, the UITextView text view, and UIButton buttons:
Get directory. Get list of files of the specified directory.
Get file meta. Get file metainformation.
Get file. Get file.
Put file. Send file.
Delete file. Delete file at server.
NOTE. When the application is initialized, authentication is executed, the buttons are not available. If the authentication is executed successfully, the buttons are available, and the text box displays the text "Authentication success".
To execute the example:
Click the Get Directory button.
Clicking the button initializes the request to get a list of folders and files in the specified root directory of the "." file resource.
As a result, the list of folders and text files contained in the directory is displayed:
Enter the file.txt file to the text box and click the Get File Meta button.
As a result, metainformation of the specified file is obtained from mobile platform server:
Enter the file.txt file to the text box and click the Get File button.
As a result, the specified file is obtained from mobile platform server:
The file.txt file is written to HomeDirectory of the application:
Create a new file1.txt file in HomeDirectory, which will be located only on a mobile device:
Enter the file1.txt file to the text box and click the Put File button.
As a result, the specified file is sent to mobile platform server:
Check if the file1.txt file is sent using the Get Directory button.
As a result, the new list of folders and files is obtained, which contains the file1.txt file:
Enter the file1.txt file to the text box and click the Delete File button.
As a result, the file1.txt file is deleted from mobile application server:
Check if the file1.txt file is deleted from mobile platform server using the Get Directory button.
As a result, the list of folders and files is obtained, which does not contain the file1.txt file:
Application code:
import UIKit
class ViewController: UIViewController {
//MARK: - Outlets
@IBOutlet weak var fileNameTextField: UITextField!
@IBOutlet weak var resultTextView: UITextView!
@IBOutlet weak var getDirectoryButton: UIButton!
@IBOutlet weak var getFileMetaButton: UIButton!
@IBOutlet weak var getFileButton: UIButton!
@IBOutlet weak var putFileButton: UIButton!
@IBOutlet weak var deleteFileButton: UIButton!
//MARK: - View life cycle
override func viewDidLoad() {
super.viewDidLoad()
self.deactivateButtons()
self.initializeFramework()
self.auth {
self.activateButtons()
self.resultTextView.text = "Authentication success"
}
}
//MARK: - Actions
@IBAction func viewTapped(_ sender: UITapGestureRecognizer) {
self.fileNameTextField.resignFirstResponder()
}
@IBAction func getDirectoryPressed(_ sender: UIButton) {
self.getDirectory { (jsonResult) in
self.resultTextView.text = String(format: "%@", jsonResult)
}
}
@IBAction func getFileMetaPressed(_ sender: UIButton) {
self.getFileMeta { (jsonResult) in
self.resultTextView.text = String(format: "%@", jsonResult)
}
}
@IBAction func getFilePressed(_ sender: UIButton) {
self.getFile { (jsonResult) in
self.resultTextView.text = String(format: "%@", jsonResult)
}
}
@IBAction func putFilePressed(_ sender: UIButton) {
self.putFile { (jsonResult) in
self.resultTextView.text = String(format: "%@", jsonResult)
}
}
@IBAction func deleteFilePressed(_ sender: UIButton) {
self.deleteFile { (jsonResult) in
self.resultTextView.text = String(format: "%@", jsonResult)
}
}
//MARK: - HHFW - Framework initialization
private func initializeFramework() {
let apiVersion: String = "v1"
let host: String = "http://testmasterfmp.fsight.cloud/"
let environment: String = "Leonid_environment"
let project: String = "Leonid_project"
let application: String = "app"
let device: String = (UIDevice.current.identifierForVendor?.uuidString)!
HHFWController.sharedInstance().initWithCredentials(
apiVersion,
host: host,
environment: environment,
project: project,
application: application,
device: device
)
}
//MARK: - HHFW - Authentication
private func auth(completion: @escaping ()->()) {
let username: String = "Leonid"
let password: String = "123123"
HHFWController.sharedInstance().auth(username, password: password){ (jsonResult) in
if let jsonDict = jsonResult as? NSDictionary,
let status = jsonDict["status"] as? String, status == "ok" {
print("Auth success")
completion()
} else {
print("Auth error")
}
}
}
//MARK: - HHFW - Get files in directory
private func getDirectory(completion: @escaping (NSDictionary)->()) {
let directory: String = "."
let mountName: String = "nfs4"
HHFWController.sharedInstance().getDirectory(directory, mountName: mountName) { (jsonResult) in
if let jsonDict = jsonResult as? NSDictionary,
let status = jsonDict["status"] as? String, status == "ok" {
print("Get directory success")
completion(jsonDict)
} else {
print("Get directory error")
}
}
}
//MARK: - HHFW - Get file meta-data
private func getFileMeta(completion: @escaping (NSDictionary)->()) {
guard let fileName: String = self.fileNameTextField.text else { return }
let mountName: String = "nfs4"
HHFWController.sharedInstance().getFileMeta(fileName, mountName: mountName) { (jsonResult) in
if let jsonDict = jsonResult as? NSDictionary,
let status = jsonDict["status"] as? String, status == "ok" {
print("Get file meta success")
completion(jsonDict)
} else {
print("Get file meta error")
}
}
}
//MARK: - HHFW - Get file
private func getFile(completion: @escaping (NSDictionary)->()) {
guard let fileName: String = self.fileNameTextField.text else { return }
let mountName: String = "nfs4"
HHFWController.sharedInstance().getFile(fileName, mountName: mountName) { (jsonResult) in
if let jsonDict = jsonResult as? NSDictionary,
let status = jsonDict["status"] as? String, status == "ok" {
print("Get file success")
completion(jsonDict)
} else {
print("Get file error")
}
}
//MARK: - HHFW - Put file
private func putFile(completion: @escaping (NSDictionary)->()) {
guard let fileName: String = self.fileNameTextField.text else { return }
let mountName: String = "nfs4"
HHFWController.sharedInstance().putFile(fileName, mountName: mountName) { (jsonResult) in
if let jsonDict = jsonResult as? NSDictionary,
let status = jsonDict["status"] as? String, status == "ok" {
print("Put file success")
completion(jsonDict)
} else {
print("Put file error")
}
}
}
//MARK: - HHFW - Delete file
private func deleteFile(completion: @escaping (NSDictionary)->()) {
guard let fileName: String = self.fileNameTextField.text else { return }
let mountName: String = "nfs4"
HHFWController.sharedInstance().deleteFile(fileName, mountName: mountName) { (jsonResult) in
if let jsonDict = jsonResult as? NSDictionary,
let status = jsonDict["status"] as? String, status == "ok" {
print("Delete file success")
completion(jsonDict)
} else {
print("Delete file error")
}
}
}
//MARK: - View methods
private func deactivateButtons() {
self.getDirectoryButton.isEnabled = false
self.getFileMetaButton.isEnabled = false
self.getFileButton.isEnabled = false
self.putFileButton.isEnabled = false
self.deleteFileButton.isEnabled = false
}
private func activateButtons() {
self.getDirectoryButton.isEnabled = true
self.getFileMetaButton.isEnabled = true
self.getFileButton.isEnabled = true
self.putFileButton.isEnabled = true
self.deleteFileButton.isEnabled = true
}
}
See also: