I am creating a new ios-plugin with capacitor to ionic 4. I can create the plugin and import to my project but i have a problem with methods on my plugin.
I send someData to my plugin and i hope my method responds a boolean value but that doesn't happen.
PLUGIN IOS
1) .SWIFT
lazy var keychain : KeychainSwift = {
return KeychainSwift()
}()
@objc func setKeychain(_ call: CAPPluginCall) {
let value = call.getString("textToSave") ?? ""
let identifier = call.getString("identifierOfKey") ?? ""
var response = false
if !identifier.isEmpty, !value.isEmpty {
keychain.set(value, forKey: identifier)
response = true
}
call.resolve([
"value": response
])
}
@objc func setKeychainWithBoolean(_ call: CAPPluginCall) {
let value = call.getString("textToSave") ?? ""
let identifier = call.getString("identifierOfKey") ?? ""
var status = call.getBool("status") ?? false
//if !identifier.isEmpty, !value.isEmpty {
// keychain.set(value, forKey: identifier)
// status = true
//}
status = true
call.resolve([
"value": status
])
}
2) .m
CAP_PLUGIN(DanielPlugin, "DanielPlugin",
CAP_PLUGIN_METHOD(setKeychain, CAPPluginReturnPromise);
CAP_PLUGIN_METHOD(setKeychainWithBoolean, CAPPluginReturnPromise);
CAP_PLUGIN_METHOD(echo, CAPPluginReturnPromise);
)
3) src/definitions.ts
setKeychain(options: { textToSave: string, identifierOfKey: string}): Promise<{value: Object }>;
setKeychainWithBoolean(options: { textToSave: string, identifierOfKey: string , status: boolean }): Promise<{value: Object }>;
echo(options: {value: string}): Promise<{value: string}>;
4) src/web.ts
async setKeychain(options: { textToSave: string, identifierOfKey: string }): Promise<{value: Object }>{
return Promise.resolve({ value: options.textToSave });}
async setKeychainWithBoolean(options: { textToSave: string, identifierOfKey: string, status: boolean }): Promise<{value: Object }>{
return Promise.resolve({ value: options.status });}
async echo(options: {value: string}): Promise<{value: string}> {
return Promise.resolve({ value: options.value });}
IONIC PROJECT
1)
onSubmit(){
this.setObject();
}
async setObject(){
const nameValue = this.dataText.get('inputName').value;
const data = await DanielPlugin.setKeychain({
textToSave: nameValue,
identifierOfKey: 'name'
});
const otherData = await DanielPlugin.setKeychainWithBoolean({
textToSave: nameValue,
identifierOfKey: 'name',
status: false
});
console.log("json response data:",data);
console.log("json response otherData:",otherData);
}
On method "setKeychain" i expect the output of "true" or "false" but I get the name I put.
On method "setKeychainWithBoolean" I expect the output "true" but always I have "false"
Plugin -> https://github.com/daniel190392/daniel-plugin-ionic