You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

react-native-keychain中getAllGenericPasswordServices使用及安全验证问题

react-native-keychain常见问题解答

1. 如何将getAllGenericPasswordServices返回结果转为服务名称数组

getAllGenericPasswordServices()的返回值是键为服务名称、值为对应凭据信息的对象,你可以通过Object.keys()方法直接提取所有服务名称组成数组,满足列表展示需求。

修改后的代码示例:

import * as Keychain from 'react-native-keychain';

export const getAllProperties = async () => {
  try {
    const allProperties = await Keychain.getAllGenericPasswordServices();
    if (allProperties) {
      // 提取所有服务名称转为数组
      const serviceNames = Object.keys(allProperties);
      console.log('所有存储的服务名称数组:', serviceNames);
      console.log('原返回值类型:', typeof allProperties);
    } else {
      console.log('No credentials stored');
    }
  } catch (error) {
    console.log("Keychain couldn't be accessed!", error);
  }
};

2. Android模拟器无需验证即可获取数据的原因及解决方法

这是模拟器默认特性导致的:

  • Android模拟器默认未设置锁屏密码或生物识别(指纹/FaceID),钥匙串的访问权限验证机制不会触发。
  • 若要开启验证,需完成两步操作:
    1. 在模拟器系统设置中添加锁屏密码或指纹(可通过AVD管理器给模拟器配置指纹);
    2. 调用钥匙链API时配置accessControl参数指定验证要求,示例如下:
      // 存储凭据时指定验证要求
      await Keychain.setGenericPassword(
        'username',
        'password',
        {
          service: 'my-service',
          accessControl: Keychain.ACCESS_CONTROL.BIOMETRY_CURRENT_SET // 要求当前已设置的生物识别验证
        }
      );
      
      // 获取凭据时会触发验证
      const credentials = await Keychain.getGenericPassword({
        service: 'my-service',
        accessControl: Keychain.ACCESS_CONTROL.BIOMETRY_CURRENT_SET
      });
      
    只有同时满足设备设置了验证方式、代码中配置了accessControl参数,获取数据时才会弹出验证窗口。

内容的提问来源于stack exchange,提问作者Sahar Toledano

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.20 10:22:49