如何使用 react-native-keychain 保存多个键值?

Muh*_*mad 6 react-native react-native-keychain

我有多个key, value,我不知道如何key, value在不同的地方存储多个。

是否有使用react-native-keychainpackage的以下代码的替代方法

await AsyncStorage.setItem('pincode',  '12345');
await AsyncStorage.setItem('userid',  '@user8383928');
await AsyncStorage.setItem('contacts',  JSON.stringify(contacts_array_of_object));
Run Code Online (Sandbox Code Playgroud)

以上每个都key, value saving可能在不同的函数和不同的时间调用。

问题在于react-native-keychain只有两个属性username, password

async () => {
  const username = 'zuck';
  const password = 'poniesRgr8';

  // Store the credentials
  await Keychain.setGenericPassword(username, password);

  try {
    // Retrieve the credentials
    const credentials = await Keychain.getGenericPassword();
    if (credentials) {
      console.log('Credentials successfully loaded for user ' + credentials.username);
    } else {
      console.log('No credentials stored');
    }
  } catch (error) {
    console.log('Keychain couldn\'t be accessed!', error);
  }
  await Keychain.resetGenericPassword();
}
Run Code Online (Sandbox Code Playgroud)

Bla*_*ack 5

1) 设置InternetCredentials

setInternetCredentials(server, username, password)接受服务器作为第一个参数。服务器可以是任何字符串。

await setInternetCredentials('pincode', 'pincode', '123456');
await setInternetCredentials('contacts', 'contacts', JSON.stringify(contacts_array_of_object));
Run Code Online (Sandbox Code Playgroud)

然后您可以使用从钥匙串中获取它

await getInternetCredentials('pincode');
Run Code Online (Sandbox Code Playgroud)

2) setGenericPassword 与服务值

setGenericPassword(username, password, [{ accessControl, accessible, accessGroup, service, securityLevel }]) 您必须指定服务值,这是用于在内部存储中存储机密的名称

await setGenericPassword('pincode', '123456', [{ service = 'pincode' }]);
Run Code Online (Sandbox Code Playgroud)

3) JSON.strigify 与 setGenericPassword

每次你想写东西时,你都会先读取密钥库,然后使用对象扩展将它与新值存储在一起。


小智 3

将它们全部放在一个对象中,然后将其另存为JSON.stringify({...})

  • 但是我们需要在不同的时间保存 (2认同)