为什么钥匙串在IOSreact native中杀死应用时重置数据?有什么理由吗

问题描述

当前,当我在第一次钥匙串上杀死该应用程序时,可以完美地持久保存数据。但是,这是第二次在应用程序中杀死钥匙串重置凭据。这是我的代码

  componentDidMount() {
        AppState.addEventListener('change',this._handleAppStateChange);
        Keychain.getGenericPassword()
        .then((credentials) => {
          console.log(credentials.username,'Credentials Log')
            if (credentials.username === '' ||  credentials.username === undefined) {
                  this.setState({isKeyClear:true,isTouchShow:false})
            } else {
                  this.setState({isKeyClear:false,isTouchShow:true});
                  // this.clickTouchID();
            }
        })
        .catch(err => {
            console.log("err: ",err);
        });
  }

在这里,我检查了触摸ID或面部ID支持

  clickTouchID  = () => {
    console.log('Click');
    TouchID.isSupported()
      .then(biometryType => {
        if(biometryType){
          if (biometryType === 'TouchID') {
            // Touch ID is supported on iOS
            // this.setCredentials(username,password);
            this.getCredentials();
          } else if (biometryType === 'FaceID') {
            // Face ID is supported on iOS
            this.getCredentials();
          } else if (biometryType === true) {
            console.log(biometryType,'Android');
            // Touch ID is supported on Android
            this.getCredentials();
          }
        }
        else{
            console.log('Not Supported');
        }
    })
    .catch(error => {
      console.log('Users device does not support Touch ID (or Face ID)',error);
      this.devicesSupporteDalert();
      // User's device does not support Touch ID (or Face ID)
      // This case is also triggered if users have not enabled Touch ID on their device
    });
  }

方法将凭据存储在钥匙串中

  setCredentials = async (username,password) => {
    return new Promise((resolve,reject) => {
        // Store the credentials
        Keychain.setGenericPassword(username,password,{accessible: Keychain.ACCESSIBLE.ALWAYS})
            .then(resp => {
                console.log(resp,'Setcredentials Response');
                // this.getCredentials();
                resolve(true)
            })
            .catch(err => {
                console.log("err: Detail",err);
                reject(err);
            });
    });
  }

方法获取钥匙串中的凭据,但条件是首先检查数据是否存在。

  getCredentials = async () => {
    return new Promise((resolve,reject) => {
        Keychain.getGenericPassword()
            .then((credentials) => {
                if (credentials.username === '' ||  credentials.username === undefined) {
                    if(isAndroid){
                      this.setState({isKeyClear:true,isTouchShow:false})
                    }
                      console.log('No credentials stored');
                      resolve(credentials);
                } else {
                      if(isAndroid){
                        this.setState({isKeyClear:false,isTouchShow:true})
                      }
                      console.log('Credentials successfully loaded for user' + credentials.username);
                        // If Touch ID authentication is successful,call the `login` api

                  TouchID.authenticate()   
                  .then(() => {
                    // If Touch ID authentication is successful,call the `login` api
                    this.setState({bioState:true});
                    let userObj = {
                      Isemail:credentials.username,Ispassword:credentials.password
                    }
                    this.onPressLogin(userObj)
                  })
                  .catch(error => {
                    console.log("error: ",error);
                    reject(error);
                  })

                }
            })
            .catch(err => {
                console.log("err: ",err);
                reject(err);
            });
    });
  }

  _handleAppStateChange = (nextAppState) => {
    if(isAndroid){
      if (nextAppState === 'background' ||  nextAppState === 'active') {
          this.clickTouchID();
          console.log('the app is closed Android');
      }
    }
    else if(nextAppState === 'inactive') {
        console.log('the app is closed IOS');
    }
  }

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)