我在 React Native 应用程序(我们称之为应用程序 A)中使用
react-native-keychain
成功存储了凭据(例如用户名和密码)。
将凭据存储在应用程序 A 中后,我尝试从另一个应用程序(我们称之为应用程序 B)检索它们。但是,我在使用
react-native-keychain
检索凭据时遇到了困难。
const storeData = async () => {
const result = await Keychain.setGenericPassword(
'credentials',
'[email protected]|test123',
{
service: 'com.demo.cred',
},
);
console.log('🚀 ~ storeCredentials ~ result:', result);
};
const getData = async () => {
const credentials = await Keychain.getGenericPassword({
service: 'com.demo.cred',
});
console.log('🚀 ~ retrieveCredentials ~ credentials:', credentials);
};
const resetData = async () => {
const result = await Keychain.resetGenericPassword({
service: 'com.demo.cred',
});
console.log('🚀 ~ resetCredentials ~ result:', result);
};
注意:从应用程序 A 存储后,我能够从应用程序 A 检索凭据,但无法从应用程序 B 检索凭据。尽管此流程适用于 iOS。
根据手动链接的文档https://www.npmjs.com/package/react-native-keychain#option-manually-1,我面临以下步骤的问题。
package com.myapp;
+ import com.oblador.keychain.KeychainPackage;
....
public class MainActivity extends extends ReactActivity {
@Override
protected List<ReactPackage> getPackages() {
return Arrays.<ReactPackage>asList(
new MainReactPackage(),
+ new KeychainPackage()
);
}
...
}
MainApplication.java
....
import com.oblador.keychain.KeychainPackage; // --> added import here
....
public class MainApplication extends Application implements ReactApplication, DefaultLifecycleObserver {
static Context activityContext;
private final ReactNativeHost mReactNativeHost =
new DefaultReactNativeHost(this) {
@Override
public boolean getUseDeveloperSupport() {
return BuildConfig.DEBUG;
}
@Override
protected List<ReactPackage> getPackages() {
@SuppressWarnings("UnnecessaryLocalVariable")
List<ReactPackage> packages = new PackageList(this).getPackages();
// Packages that cannot be autolinked yet can be added manually here, for example:
// packages.add(new MyReactNativePackage());
packages.add(new com.ekk.motorcity.MyAppPackage());
packages.add(new KeychainPackage()); // --> added package here
return packages;
}
....
我尝试在我的应用程序中添加类似的内容,但出现错误
Native module RNKeychainManager tried to override KeychainModule. Check the getPackages() method in MainApplication.java, it might be that module is being created twice. If this was your intention, set canOverrideExistingModule=true. This error may also be present if the package is present only once in getPackages () but is also automatically added later during build time by autolinking. Try removing the existing entry and rebuild.
附上错误截图以供参考错误 - RNKeychainManager
社区针对此问题提供的任何建议或修复,以及任何见解或指导,我们将不胜感激。 谢谢!
尝试过 - 自动链接,诸如react-native-keychain
和
Keychain.SECURITY_LEVEL
之类的选项