9
0
MetaPluginIosDemo/MetaPluginDemo/ViewController.swift

246 lines
7.6 KiB
Swift
Raw Normal View History

/*
* Created by WebID Solutions GmbH | www.webid-solutions.de.
* See the file "LICENSE" for the full license governing this code.
*/
import UIKit
import WebIdIosSdk
import WebIdPluginCore
import WebIdMetaPlugin
import WebIdUiKitComponents
import WebIdVideoIdentProductPlugin
import WebIdPayOnServerProductPlugin
import WebIdEIdOnServerProductPlugin
import WebIdAutoIdentOnServerProductPlugin
/// Demo App which starts the ``VideoIdentProductPlugin`` and handles its result
class ViewController:
UIViewController,
UIPickerViewDataSource,
UIPickerViewDelegate,
IProductPluginWebidDelegate {
var metaPluginExecutionMode: EMetaPluginMode = .live
@IBOutlet weak var modePicker: UIPickerView!
@IBOutlet weak var cbAutoIdOnServer: Checkbox!
@IBOutlet weak var cbPayOnServer: Checkbox!
@IBOutlet weak var cbEIdOnServer: Checkbox!
@IBOutlet weak var log: UITextView!
override func viewDidLoad() {
super.viewDidLoad()
modePicker.delegate = self
modePicker.dataSource = self
cbAutoIdOnServer.textView.textColor = .black
cbPayOnServer.textView.textColor = .black
cbEIdOnServer.textView.textColor = .black
cbAutoIdOnServer.textView.text = "AutoIdOnServer"
cbPayOnServer.textView.text = "PayOnServer"
cbEIdOnServer.textView.text = "EIdOnServer"
}
@IBAction func touchStartButton(_ sender: Any) {
clearLog()
writeLog(entry: "Starting…")
createCoreSdk()
}
// MARK: Create MetaPlugin
var metaPlugin: MetaPlugin!
/// Creates the the meta-plugin instance from the existing credentials.
private func createCoreSdk() {
writeLog(entry: "Creating Meta Plugin…")
// actual creation of the core SDK
do {
let env = EWebIDEnv.TEST
metaPlugin = MetaPlugin(
username: env.username,
apiKey: env.apiKey,
env: try WebIdSdkEnvironment(
url: env.uri,
pinningCertificates: env.pinningCerts
),
plugins: getChosenPlugins()
)
writeLog(entry: "Meta Plugin creation successful")
verifyActionId()
} catch {
writeLog(entry: "Meta Plugin creation failed")
}
}
// MARK: Verify Action-ID
/// Calls the verify action-id method asynchronously to start the process.
private func verifyActionId() {
writeLog(entry: "Verifying Action-ID…")
Task {
do {
let result = try await metaPlugin.verify(for: Credentials.actionId)
verifyActionIdCallback(result: .success(result))
} catch {
verifyActionIdCallback(result: .failure(error))
}
}
}
/**
* Callback for the asynchronous verify call.
*
* - Parameters:
* - result The result, containing the VerifyActionIdResult of the call
*/
@MainActor
private func verifyActionIdCallback(result: Result<VerifyActionIdResult, Error>) {
switch result {
case .success(let verifyActionIdResult):
writeLog(entry: "Verification successful")
startMetaPlugin(with: verifyActionIdResult)
case .failure(let error):
writeLog(entry: "Verification failed: \(error.localizedDescription)")
}
}
// MARK: Start MetaPlugin
/**
* Reads the given Configs and starts the plugin.
*
* - Parameters:
* - verifyActionIdResult: result of the verify action-id call
*/
private func startMetaPlugin(with verifyActionIdResult: VerifyActionIdResult) {
writeLog(entry: "Starting MetaPlugin...")
do {
try metaPlugin.startPlugin(
parentVC: self,
delegate: self,
lightTheme: CustomizedPluginTheme.getLightVersion(),
darkTheme: CustomizedPluginTheme.getDarkVersion()
)
} catch {
writeLog(entry: "Start Failed:\n\(error.localizedDescription)")
}
}
/**
* Successful delegate callback for the plugin.
*
* - Parameters:
* - additionalResult: extra info from the plugin
* - processFinished: irrelevant for this plugin
*/
func finishedSuccess(additionalResult: String?, processFinished: Bool) {
writeLog(entry: "Product Journey finished successfully")
}
/**
* Failed delegate callback for the plugin with a plugin-specific error.
*
* - Parameters:
* - failreason: the reason for the failure of the plugin, handle according to documentation
* - additionalResult: extra info from the plugin
*/
func finishedFailed(
failreasonContainer: WebIdPluginCore.PluginFailReasonContainer,
additionalResult: String?,
faultOriginator: String
) {
if let container = failreasonContainer as? AutoIdOnServerProductPluginFailReasonContainer {
writeLog(entry: "Product journey finished with failure:\n\(container.failreason)")
}
}
/**
* Failed delegate callback for the plugin with a generic error.
*
* - Parameters:
* - failreason: the reason for the failure of the plugin, handle according to documentation
* - additionalResult: extra info from the plugin
*/
func finishedFailed(
failreason: EProductPluginErrors,
additionalResult: String?,
faultOriginator: String
) {
writeLog(entry: "Product journey finished with failure:\n\(failreason.self)")
}
// MARK: Plugin chooser
private func getChosenPlugins() -> [any IProductPluginWebId] {
var plugins = [any IProductPluginWebId]()
if cbAutoIdOnServer.isChecked {
plugins.append(AutoIdentOnServerProductPlugin())
}
if cbPayOnServer.isChecked {
plugins.append(PayOnServerProductPlugin())
}
if cbEIdOnServer.isChecked {
plugins.append(EidOnServerProductPlugin())
}
return plugins
}
// MARK: PickerView delegate
func numberOfComponents(in pickerView: UIPickerView) -> Int {
return 1
}
func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
return EMetaPluginMode.allCases.count
}
// The data to return for the row and component (column) that's being passed in
func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
return EMetaPluginMode.allCases[row].rawValue
}
// Capture the picker view selection
func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
metaPluginExecutionMode = EMetaPluginMode.allCases[row]
}
// MARK: logging methods
/**
* Add an entry to the log.
*
* - Parameters:
* - entry: the new line for the log
*/
private func writeLog(entry: String) {
log.text = log.text + entry + "\n"
}
/// Resets the text log of this activity.
private func clearLog() {
log.text = ""
}
func infoAlert(_ message: String, go: ((UIAlertAction) -> Void)? = nil) {
let alertController = UIAlertController(
title: "Info",
message: message,
preferredStyle: UIAlertController.Style.alert
)
alertController.addAction(UIAlertAction(
title: "OK",
style: UIAlertAction.Style.default,
handler: go
))
self.present(
alertController,
animated: true,
completion: nil
)
}
}