Hi,
With Supermamon remarks, I do a little script to manage the keychain; following it, my module alert.
About this module, I don’t understand why I have to use a function to avoid error (if I put directly the code in the module) on the await alert.
// If you want all your yet created keys:
// + create the Keychain with key "42keys42"
// + set its values (example for 3 keys) with "nameOfTheKey1&@#nameOfTheKey2&@#nameOfTheKey3""
const malAdd = importModule('/malAdd/alert')
title = "Keychain"
msg = "...is a secure storage for global variables."
inputs = [["Key name",""],["[Key value]",""]]
options = ["set","get","remove","contains"]
key = await malAdd.alert(title, msg, inputs, options)
alert = new Alert
alert.addAction("OK")
keys = Keychain.get("42keys42");keysList = keys.split("&@#");keysList.sort()
switch (key[0]) {
case 0: if (key[1] == "") {alert.title = "Alert";alert.message = "Key name is required for this method.";alert.present()}
else {Keychain.set(key[1],key[2]);
if (Keychain.contains("42keys42")) {key[1] = Keychain.get("42keys42") + "&@#" + key[1]}
Keychain.set("42keys42",key[1])}
break;
case 1: if (key[1] == "") {
msg = "Key?"
choice = await malAdd.alert("",msg,"",keysList)
alert.title = "Value of " + keysList[choice] + ":";alert.message = Keychain.get(keysList[choice])}
else {alert.title = "Value of " + key[1] + ":";alert.message = Keychain.get(key[1])}
alert.present()
break;
case 2: if (key[1] == "") {
title = "Warning";msg = "You will remove the selected key."
choice = await malAdd.alert(title,msg,"",keysList)
alert.title = "Are you sure?";alert.addCancelAction("Cancel")
confirm = await alert.present()
if (confirm != - 1) {
Keychain.remove(keysList[choice])
Keychain.set("42keys42",Keychain.get("42keys42").replace(keysList[choice] + "&@#","").replace("&@#" + keysList[choice], ""))}}
else {Keychain.remove(key[1])
Keychain.set("42keys42",Keychain.get("42keys42").replace(keysList[choice] + "&@#","").replace("&@#" + keysList[choice], ""))}
break;
case 3: if (key[1] == "") {
keys = Keychain.get("42keys42");keysList = keys.split("&@#");keysList.sort()
msg = "Keys"
await malAdd.alert("",msg,"",keysList)}
else {alert.message = Keychain.contains(key[1]).toString();alert.present()}
break;}
And the module:
module.exports.alert = (title,msg,inputs,options) => {
async function alert(title,msg,inputs,options) {
var alert = new Alert()
if (title) {alert.title = title}
if (msg) {alert.message = msg}
if (inputs.length > 0) {for (input of inputs) {alert.addTextField(input[0],input[1])}}
if (options.length >0) {for (option of options) {alert.addAction(option)}}
let response = await alert.present();
let out = []
out[0] = response
for (i=0;i<inputs.length;i++) {out[i + 1] = alert.textFieldValue(i)}
return out}
let out2 = alert(title, msg, inputs, options)
return out2}