To get a resources scheme, create the HHFWResources application, which uses 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. |
openBase (_: key:) | The method opens connection with database at the specified path. |
resources (_: handler:) | The method gets schemes of available resources and automatically creates corresponding tables in the database. |
Auxiliary methods:
Method name | Brief description |
getURL (for:) | The method returns URL for the file with the specified name in the Documents folder. Type of returned data: URL |
The HHFWResources application includes one screen, the UITextView text view and the UIButton button:
Load resources. Get resources scheme.
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 display the text "Authentication success".
To execute the example, click the Load Resources button. Clicking the button establishes a connection with database. If the database file is not found at the specified path, a new file is created.
After the connection is successfully established, the resources scheme is loaded. The tables containing data on resources structure are created in the local database. If such scheme already exists in the database, the current and requested schemes are compared. The scheme corresponding to the structure on a mobile platform server has a higher priority if the compared schemes mismatch.
Application code:
import UIKit
class ViewController: UIViewController {
// Determine output variables
@IBOutlet weak var resultTextView: UITextView!
@IBOutlet weak var loadResourcesButton: UIButton!
// Redetermine method
override func viewDidLoad() {
super.viewDidLoad()
self.deactivateButtons()
self.initializeFramework()
self.auth {
self.activateButtons()
self.resultTextView.text = "Authentication success"
}
}
// Set method that will be executed on button click
@IBAction func loadResourcesPressed(_ sender: UIButton) {
let databaseName = "database.sqlite"
if self.openDatabase(databaseName) {
self.loadResourcesScheme(databaseName) { (jsonResult) in
self.resultTextView.text = String(format: "%@", jsonResult)
}
}
}
// 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 = "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")
}
}
}
// Open or create a database
private func openDatabase(_ databaseName: String) -> Bool {
let fullDatabaseURL = self.getURL(for: databaseName)
return HHFWController.sharedInstance().openBase(fullDatabaseURL.path, key: "")
}
// Load resources scheme
private func loadResourcesScheme(_ databaseName: String, completion: @escaping (NSDictionary)->()) {
HHFWController.sharedInstance().resources(databaseName) { (jsonResult) in
if let jsonDict = jsonResult as? NSDictionary,
let status = jsonDict["status"] as? String, status == "ok" {
print("Load resources success")
completion(jsonDict)
} else {
print("Load resources error")
}
}
}
// Call auxiliary classes
private func getURL(for fileName: String) -> URL {
let paths: [URL] = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)
let fileURL: URL = paths[0].appendingPathComponent(fileName)
return fileURL
}
// Set methods to display application user interface elements
private func deactivateButtons() {
self.loadResourcesButton.isEnabled = false
}
private func activateButtons() {
self.loadResourcesButton.isEnabled = true
}
}
See also:
Examples of iOS Framework Use | Examples of Working with Resources