A request can be executed synchronously and asynchronously:
Synchronous requests. They return values after method execution.
Asynchronous requests. They return no value and the application moves to the next method. The response is generated as soon as available.
To execute synchronous or asynchronous uniform request to resource without loading to database, create the HHFWRequestRetry application that uses the framework methods:
Method name | Brief description |
initWithCredentials (_: host: environment: project: device:) | The method initializes the framework. |
auth (_: password:) | The method authenticates the user by login and password. |
setDownloadPath(_:) | The method sets the path to the folder in the mobile application directory, which stores files with the current resource downloading state. |
useDownload(_:) | The method determines whether to resume resource downloading. NOTE. The application code uses the tableStream/tableStreamAsync method, for which the TableCallParams auxiliary class is determined with the retryCount and retryIntervalSec properties. |
request (_: requestCallParams: handler:) | The method sends a synchronous uniform request to resource without loading to database. |
requestAsync (_: requestCallParams: handler:) | The method sends an asynchronous uniform request to resource without loading to database. |
Auxiliary method:
Method name | Brief description |
getURL (forFolder:) | The method returns URL of the folder with specified name in the Documents folder. Type of returned data: URL |
The HHFWRequestRetry application includes one screen, the UITextView text view and the UIButton button:
Perform request. Execute synchronous or asynchronous request to resource without loading to database.
NOTE. When the application is initialized, authentication is executed, the button is not available. If the authentication is executed successfully, the button is available, and the text box displays the text "Authentication success".
To execute the example, click the Perform Request button. Clicking the button initializes a request to resource. A mobile platform server returns corresponding data.
Application code with synchronous request execution:
import UIKit
class ViewController: UIViewController {
// Determine output variables
@IBOutlet weak var resultTextView: UITextView!
@IBOutlet weak var requestButton: UIButton!
// Redetermine method
override func viewDidLoad() {
super.viewDidLoad()
self.deactivateButtons()
self.initializeFramework()
self.auth {
self.activateButton()
self.resultTextView.text = "Authentication success"
}
}
// Set method that will be executed on button click
@IBAction func requestPressed(_ sender: UIButton) {
self.performRequest { (jsonDict) in
self.resultTextView.text = String(format: "%@", jsonDict)
}
}
// Initialize framework
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 device: String = (UIDevice.current.identifierForVendor?.uuidString)!
HHFWController.sharedInstance().initWithCredentials(
apiVersion,
host: host,
environment: environment,
project: project,
device: device
)
}
// Authenticate
private func auth(completion: @escaping ()->()) {
let username: String = "test"
let password: String = "test123"
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")
}
}
}
private func setResumingDownload(_ value: Bool) {
let path = self.getURL(forFolder: "DownloadPath").path
HHFWController.sharedInstance().setDownloadPath(path)
HHFWController.sharedInstance().useDownload(value)
}
// Perform the Request request
private func performRequest(completion: @escaping (NSDictionary)->()) {
let resourceName = "FRUITS"
let requestCallParams = RequestCallParams(defaultProperty: ())
requestCallParams?.retryCount = 10
requestCallParams?.retryIntervalSec = 1
HHFWController.sharedInstance().request(resourceName, requestCallParams: requestCallParams) { (jsonResult) in
if let jsonDict = jsonResult as? NSDictionary,
let status = jsonDict["status"] as? String, status == "ok" {
print("Request success")
completion(jsonDict)
} else {
print("Request error")
}
}
}
// Set methods to display application user interface elements
private func deactivateButtons() {
self.requestButton.isEnabled = false
}
private func activateButton() {
self.requestButton.isEnabled = true
}
// Call auxiliary classes
private func getURL(forFolder folderName: String) -> URL {
let paths: [URL] = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)
let folderURL: URL = paths[0].appendingPathComponent(folderName, isDirectory: true)
return folderURL
}
}
Application code with asynchronous request execution:
import UIKit
class ViewController: UIViewController {
// Determine output variables
@IBOutlet weak var resultTextView: UITextView!
@IBOutlet weak var requestButton: UIButton!
// Redetermine method
override func viewDidLoad() {
super.viewDidLoad()
self.deactivateButtons()
self.initializeFramework()
self.auth {
self.activateButton()
self.resultTextView.text = "Authentication success"
}
}
// Set method that will be executed on button click
@IBAction func requestPressed(_ sender: UIButton) {
self.performRequest { (jsonDict) in
self.resultTextView.text = String(format: "%@", jsonDict)
}
}
// Initialize framework
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 device: String = (UIDevice.current.identifierForVendor?.uuidString)!
HHFWController.sharedInstance().initWithCredentials(
apiVersion,
host: host,
environment: environment,
project: project,
device: device
)
}
// Authenticate
private func auth(completion: @escaping ()->()) {
let username: String = "test"
let password: String = "test123"
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")
}
}
}
private func setResumingDownload(_ value: Bool) {
let path = self.getURL(forFolder: "DownloadPath").path
HHFWController.sharedInstance().setDownloadPath(path)
HHFWController.sharedInstance().useDownload(value)
}
// Perform the RequestAsync request
private func performRequest(completion: @escaping (NSDictionary)->()) {
let resourceName = "FRUITS"
let requestCallParams = RequestCallParams(defaultProperty: ())
requestCallParams?.retryCount = 10
requestCallParams?.retryIntervalSec = 1
HHFWController.sharedInstance().requestAsync(resourceName, requestCallParams: requestCallParams) { (jsonResult) in
if let jsonDict = jsonResult as? NSDictionary,
let status = jsonDict["status"] as? String, status == "ok" {
print("Request success")
completion(jsonDict)
} else {
print("Request error")
}
}
}
// Set methods to display application user interface elements
private func deactivateButtons() {
self.requestButton.isEnabled = false
}
private func activateButton() {
self.requestButton.isEnabled = true
}
// Call auxiliary classes
private func getURL(forFolder folderName: String) -> URL {
let paths: [URL] = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)
let folderURL: URL = paths[0].appendingPathComponent(folderName, isDirectory: true)
return folderURL
}
}
See also:
Examples of iOS Framework Use | Examples of Working with Resources