iOS - Method Module
To call a MethodModule
, a Tecton Caliper extension is required. Please refer to the
Caliper guides to get started.
The MethodModule
interface provides the ability to create native methods which can be
called from Q2MobileCore through Tecton. The invoked Tecton method will execute the native
code written inside the type implementing method module.
This native code is able to execute native frameworks, third-party SDKs and return a response back to the Tecton method call.
Implementation
Implementing a MethodModule
is straightforward—create a class inherited from NSObject
and conforming to the MethodModule protocol requirements.
import Q2ModuleInterfaces
class CustomMethodModule: NSObject, MethodModule {
var moduleInvocationData: ModuleInvocationData!
var moduleDelegate: ModuleDelegate?
var moduleDataSource: ModuleDataSource?
// Example: Javascript Method to Native Method (@objc)
@objc func yourJSMethodName(data: MethodModuleData, callback: MethodModuleCallback) {
let parameterValue = data.dataDictionary["parameterName"] as? String ?? "unknown"
let response = MethodModuleResponse(withResponse: parameterValue)
callback.completion(response)
}
// Example: Javascript Method to retrieve token (@objc)
@objc func getQ2Token(data: MethodModuleData, callback: MethodModuleCallback) {
let token = moduleDataSource?.user?.token ?? "nil"
let response = MethodModuleResponse(withResponse: token)
callback.completion(response)
}
}
Invocation
To invoke native methods defined in your native method module, use Tecton - callMethod capability in your online extension.
tecton.actions
.callMethod('moduleIdentifier', 'methodName', { parameterName: 'parameterValue' })
.then(response => {
//was successful at calling method
})
.catch(error => {
//could not call method
});
moduleIdentifier
is configured by Q2 on module creation.methodName
is the name of the @objc method defined by your module. You can define multiple methods.
Example: A module called Math can have multiple methods, like: sum, multiply, divide etc.