To work with a digital signature, create the HHFWDss 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. |
getCertificatesWithPassword (_: handler:) | The method returns the certificates list. |
signCertificate (withResource: table: params: column: hhiveId: password: certificateId: certificatePin: handler:) | The method sends document to sign. |
signs (handler:) | The method returns the list of digital certificates, which were created by the current user. |
remove (withSignId: handler:) | The method removes digital signature. |
Auxiliary methods:
Method name | Brief description |
getSignatureId(from:) | The method returns value for the signature_id key from JSON result of the query for resource signature. Type of returned data: Int? |
parseServerJSONResponse(json:) | The method returns the result of converting the NSDictionary json dictionary to the [String: Any]? dictionary. Type of returned data: [String: Any]? |
getResponseStatus(from:) | The method returns value for the "status" key from JSON result of the query. Type of returned data: String? |
The HHFWDss application includes one screen, the UITextView text view, the UITextField text box, and UIButton buttons:
Get certificates. Get a list of available certificates.
Get signatures. Get a list of signed cells.
Sign document. Cell signature.
Remove signature. Remove cell signature.
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 Certificates button.
Clicking the button requests the list of all user certificates. The response contains certificates available to the current user. To sign cells with a specific certificate in tables, the certificateId certificate identifier is used.
Click the Get Signatures button.
Clicking the button returns the history of signatures created by the current user. The list of signatures history is empty.
Click the Sign Document button.
Clicking the button initializes the cell signature with the parameters: the GetBudgetPlanning resource, the BudgetPlanning table, the {} parameters, the Number column, hhiveId = 1. The certificate with the 12831 identifier is also selected. A mobile platform server returns signature identifier {signature_id: 121}.
Click the Get Signatures button to get signatures history.
The signatures history list displays a new signature.
Enter the 121 signature identifier to remove into the text box.
Click the Remove Signature button.
Clicking the button removes the signature with the 121 specified identifier. If the removal was successful, a mobile platform server returns an empty response with the ok status.
To check if the signature is removed, click the Get Signatures button. Clicking the button returns the list of signatures history that were created by the current user. The list does not contain signature with the 121 identifier.
Application code:
import UIKit
class ViewController: UIViewController {
// Determine output variables
@IBOutlet weak var signatureIDTextField: UITextField!
@IBOutlet weak var resultTextView: UITextView!
@IBOutlet weak var getCertificatesButton: UIButton!
@IBOutlet weak var signButton: UIButton!
@IBOutlet weak var getSignsButton: UIButton!
@IBOutlet weak var removeSignButton: 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 getCertificatesPressed(_ sender: UIButton) {
self.getCertificatesDSS { (jsonResult) in
self.resultTextView.text = String(format: "%@", jsonResult)
}
}
@IBAction func signPressed(_ sender: UIButton) {
self.signDSS { (jsonResult) in
self.resultTextView.text = String(format: "%@", jsonResult)
if let signatureId = self.getSignatureId(from: jsonResult) {
self.signatureIDTextField.text = String(signatureId)
} else {
self.signatureIDTextField.text = ""
}
}
}
@IBAction func getSignsPressed(_ sender: UIButton) {
self.getSignsDSS { (jsonResult) in
self.resultTextView.text = String(format: "%@", jsonResult)
}
}
@IBAction func removeSignPressed(_ sender: UIButton) {
if let signatureId = Int(self.signatureIDTextField.text ?? "") {
self.removeSign(signatureId) { (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")
}
}
}
// Get list of available certificates
private func getCertificatesDSS(completion: @escaping (NSDictionary)->()) {
let dssPassword = "Saprun123"
HHFWController.sharedInstance().getCertificatesWithPassword(dssPassword) { (jsonResult) in
if let jsonDict = jsonResult as? NSDictionary,
let status = jsonDict["status"] as? String, status == "ok" {
print("Get certificates success")
completion(jsonDict)
} else {
print("Get certificates error")
}
}
}
// Sign resource with certificate
private func signDSS(completion: @escaping (NSDictionary)->()) {
let resourceName = "GetBudgetPlanning"
let tableName = "BudgetPlanning"
let params = "{}"
let column = "Number"
let hhiveId = "1"
let dssPassword = "Saprun123"
let certificateId = "12831"
let certificatePin = "123"
HHFWController.sharedInstance().signCertificate(
withResource: resourceName,
table: tableName,
params: params,
column: column,
hhiveId: hhiveId,
password: dssPassword,
certificateId: certificateId,
certificatePin: certificatePin
) { (jsonResult) in
if let jsonDict = jsonResult as? NSDictionary,
let status = jsonDict["status"] as? String, status == "ok" {
print("Sign resource success")
completion(jsonDict)
} else {
print("Sign resource error")
}
}
}
// Get signatures
private func getSignsDSS(completion: @escaping (NSDictionary)->()) {
HHFWController.sharedInstance().signs { (jsonResult) in
if let jsonDict = jsonResult as? NSDictionary,
let status = jsonDict["status"] as? String, status == "ok" {
print("Signs success")
completion(jsonDict)
} else {
print("Signs error")
}
}
}
// Delete signature
private func removeSign(_ signatureId: Int, completion: @escaping (NSDictionary)->()) {
HHFWController.sharedInstance().remove(withSignId: signatureId) { (jsonResult) in
if let jsonDict = jsonResult as? NSDictionary,
let status = jsonDict["status"] as? String, status == "ok" {
print("Remove sign success")
completion(jsonDict)
} else {
print("Remove sign error")
}
}
}
// Call auxiliary classes
private func getSignatureId(from json: NSDictionary) -> Int? {
var resultSignatureId: Int? = nil
let jsonDict = parseServerJSONResponse(json: json)
if self.getResponseStatus(from: jsonDict) == "ok" {
if let result = jsonDict?["result"] as? [String: Any],
let raw = result["raw"] as? [String:Any],
let signatureId = raw["signature_id"] as? Int {
resultSignatureId = signatureId
}
}
return resultSignatureId
}
func parseServerJSONResponse(json: NSDictionary) -> [String: Any]? {
return json as? [String: Any]
}
func getResponseStatus(from json: [String: Any]?) -> String? {
return json?["status"] as? String
}
// Set methods to display application user interface elements
private func deactivateButtons() {
self.getCertificatesButton.isEnabled = false
self.signButton.isEnabled = false
self.getSignsButton.isEnabled = false
self.removeSignButton.isEnabled = false
}
private func activateButtons() {
self.getCertificatesButton.isEnabled = true
self.signButton.isEnabled = true
self.getSignsButton.isEnabled = true
self.removeSignButton.isEnabled = true
}
}
See also: