Secure Data (Keystore)

API Reference: UI.SecureData

Provides encrypt and securely store data locally on the device. Some values to save:

  • Login data

  • Authentication token

On iOS Key Services is being used to store securely small chunks of data on behalf of the user & on Android the Android Keystore system lets you store cryptographic keys in a container to make it more difficult to extract from the device.

TypeScript code blocks include examples of how to implement, override and components within the theme. You can create page with the UI Editor to make your page compatible with theming and then you can implement themable components programmatically. Once the page is created with the UI Editor, it generates a class under scripts/generated/pages. You can then extend that class with the following TypeScript classes.

TypeScript
TypeScript
import PageSampleDesign from 'generated/pages/pageSample';
import FlexLayout = require('sf-core/ui/flexlayout');
import Application = require('sf-core/application');
import Button = require('sf-core/ui/button');
import SecureData = require('sf-core/global/securedata');
//You should create new Page from UI-Editor and extend with it.
export default class Sample extends PageSampleDesign {
mySecureData: SecureData;
btnSave: Button;
btnDel: Button;
btnRead: Button;
constructor() {
super();
// Overrides super.onShow method
this.onShow = onShow.bind(this, this.onShow.bind(this));
// Overrides super.onLoad method
this.onLoad = onLoad.bind(this, this.onLoad.bind(this));
this.layout.flexDirection = FlexLayout.FlexDirection.COLUMN;
this.layout.justifyContent = FlexLayout.JustifyContent.CENTER;
this.layout.alignItems = FlexLayout.AlignItems.CENTER;
}
}
/**
* @event onShow
* This event is called when a page appears on the screen (everytime).
* @param {function} superOnShow super onShow function
* @param {Object} parameters passed from Router.go function
*/
function onShow(superOnShow: () => void) {
const { headerBar } = this;
superOnShow();
Application.statusBar.visible = false;
headerBar.visible = false;
}
/**
* @event onLoad
* This event is called once when page is created.
* @param {function} superOnLoad super onLoad function
*/
function onLoad(superOnLoad: () => void) {
superOnLoad();
//@ts-ignore
this.mySecureData = new SecureData({
ios: {
service: "com.myapp.serviceparameter"
},
key: "keyparamater"
});
let isSaved = false;
this.btnSave = new Button({
text: "Save",
onPress: () => {
this.mySecureData.save({ value: "password" }).then((resolvedValue) => {
isSaved = true;
}, (error) => {
console.log(error);
});
}
});
this.btnDel = new Button({
text: "Delete",
onPress: () => {
if (isSaved) { // for Android this must be checked
this.mySecureData.delete({ value: "top secretpassword" }).then((resolvedValue) => {
console.log(resolvedValue);
}, (error) => {
console.log(error);
});
isSaved = false;
}
else {
alert("There is no data to delete.")
}
}
});
this.btnRead = new Button({
text: "Read",
onPress: () => {
if (isSaved) { // for Android this must be checked
this.mySecureData.read().then((resolvedValue) => {
console.log(resolvedValue);
}, (error) => {
console.log(error);
});
}
else {
alert("There is no data to read.")
}
}
});
this.layout.addChild(this.btnSave, "btnSave", ".sf-button", function (userProps) {
userProps.width = 250;
return userProps;
});
this.layout.addChild(this.btnDel, "btnDel", ".sf-button", function (userProps) {
userProps.width = 250;
return userProps;
});
this.layout.addChild(this.btnRead, "btnRead", ".sf-button", function (userProps) {
userProps.width = 250;
return userProps;
});
}