import React, { Component } from 'react'; import { KeyboardAvoidingView, StyleSheet, Text, TextInput, TouchableHighlight, View, } from 'react-native'; import * as Keychain from 'react-native-keychain'; export default class KeychainExample extends Component { state = { username: '', password: '', status: '', }; save() { Keychain .setSecurePassword('myService', this.state.username, this.state.password) .then(() => { this.setState({ status: 'Credentials saved!' }); }) .catch((err) => { this.setState({ status: 'Could not save credentials, ' + err }); }); } load() { Keychain .getSecurePassword('myService') .then((credentials) => { if (credentials) { this.setState({ ...credentials, status: 'Credentials loaded!' }); } else { this.setState({ status: 'No credentials stored.' }); } }) .catch((err) => { this.setState({ status: 'Could not load credentials. ' + err }); }); } reset() { Keychain .resetGenericPassword() .then(() => { this.setState({ status: 'Credentials Reset!', username: '', password: '' }); }) .catch((err) => { this.setState({ status: 'Could not reset credentials, ' + err }); }); } render() { return ( Keychain Example Username this.setState({ username: event.nativeEvent.text })} /> Password this.setState({ password: event.nativeEvent.text })} /> {!!this.state.status && ({this.state.status})} this.save()} style={styles.button}> Save this.load()} style={styles.button}> Load this.reset()} style={styles.button}> Reset ); } } const styles = StyleSheet.create({ container: { flex: 1, alignItems: 'center', justifyContent: 'center', backgroundColor: '#F5FCFF' }, content: { width: 250, }, title: { fontSize: 28, fontWeight: '200', textAlign: 'center', marginBottom: 20, }, field: { marginVertical: 5, }, label: { fontWeight: '500', fontSize: 15, marginBottom: 5, }, input: { borderWidth: StyleSheet.hairlineWidth, borderColor: '#ccc', backgroundColor: 'white', height: 32, fontSize: 14, padding: 8, }, status: { color: '#333', fontSize: 12, marginTop: 15, }, buttons: { flexDirection: 'row', justifyContent: 'space-between', marginTop: 20, }, button: { borderRadius: 3, overflow: 'hidden', }, save: { backgroundColor: '#0c0', }, load: { backgroundColor: '#333', }, reset: { backgroundColor: '#c00', }, buttonText: { color: 'white', fontSize: 14, paddingHorizontal: 16, paddingVertical: 8, } });