To execute a uniform request to resource without loading to database, create the HHFWRequest application, which uses 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. |
request (_: requestCallParams: handler:) | The method sends a uniform request to resource without loading to database. |
The HHFWRequest application includes one screen, the UITextView text view and the UIButton button:
Perform request. Execute 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. The mobile platform server returns corresponding data.
Application code:
import UIKit
class ViewController: UIViewController {
//MARK: - Outlets
@IBOutlet weak var resultTextView: UITextView!
@IBOutlet weak var requestButton: UIButton!
//MARK: - View life cycle
override func viewDidLoad() {
super.viewDidLoad()
self.deactivateButtons()
self.initializeFramework()
self.auth {
self.activateButton()
self.resultTextView.text = "Authentication success"
}
}
//MARK: - Actions
@IBAction func requestPressed(_ sender: UIButton) {
self.performRequest { (jsonDict) in
self.resultTextView.text = String(format: "%@", jsonDict)
}
}
//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 - Request
private func performRequest(completion: @escaping (NSDictionary)->()) {
let resourceName = "study"
let requestCallParams = RequestCallParams(defaultProperty: ())
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")
}
}
}
//MARK: - View methods
private func deactivateButtons() {
self.requestButton.isEnabled = false
}
private func activateButton() {
self.requestButton.isEnabled = true
}
}
To execute a uniform request to resource without loading to database, create the HHFWRequestAsync application, which uses 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 |
requestAsync (_: requestCallParams: handler:) | The method sends uniform request to resource without loading to database |
The HHFWRequestAsync application includes one screen, the UITextView text view and the UIButton button:
Perform async request. Execute 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 Async Request button. Clicking the button initializes a request to resource. The mobile platform server returns corresponding data.
Application code:
import UIKit
class ViewController: UIViewController {
//MARK: - Outlets
@IBOutlet weak var resultTextView: UITextView!
@IBOutlet weak var requestButton: UIButton!
//MARK: - View life cycle
override func viewDidLoad() {
super.viewDidLoad()
self.deactivateButtons()
self.initializeFramework()
self.auth {
self.activateButton()
self.resultTextView.text = "Authentication success"
}
}
//MARK: - Actions
@IBAction func requestPressed(_ sender: UIButton) {
self.performRequest { (jsonDict) in
self.resultTextView.text = String(format: "%@", jsonDict)
}
}
//MARK: - HHFW - Framework initialization
private func initializeFramework() {
let apiVersion: String = "v1"
let host: String = "http://testmasterfmp.fsight.cloud/"
let environment: String = "DocumentationExampleEnv"
let project: String = "DocumentationExampleProj"
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 = "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")
}
}
}
//MARK: - HHFW - Request
private func performRequest(completion: @escaping (NSDictionary)->()) {
let resourceName = "FRUITS"
let requestCallParams = RequestCallParams(defaultProperty: ())
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")
}
}
}
//MARK: - View methods
private func deactivateButtons() {
self.requestButton.isEnabled = false
}
private func activateButton() {
self.requestButton.isEnabled = true
}
}
See also:
Examples of iOS Framework Use | Examples of Working with Resources