react-native-keychain: Failure on setGenericPassword()

Attempting to get setup with react-native-keychain but I am receiving the error below.

TypeError: Cannot read property 'setGenericPasswordForService' of undefined(…)

I’ve installed the npm library and linked it with rnpm. I checked my Libraries folder in XCode to confirm RNKeychain.xcodeproj was installed and I can see the library installed in the Linked Binaries as well.

import Keychain from 'react-native-keychain';

export default class Init extends Component {
  componentDidMount() {
        var username = 'zuck';
    var password = 'poniesRgr8';

    Keychain
        .setGenericPassword(username, password)
        .then(function() {
                console.log('Credentials saved successfully!');
          }).catch(function(error) {
            console.log('Could not set Generic Password. ', error);
          });
  }
}

About this issue

  • Original URL
  • State: closed
  • Created 8 years ago
  • Reactions: 10
  • Comments: 19

Most upvoted comments

I’m getting this error on "react-native": "0.40.0","react-native-keychain": "^1.1.0" and fixes with import * as Keychain from 'react-native-keychain'; instead of import Keychain from 'react-native-keychain';

Thanks for the response @akullpp I did this before but what i was doing wrong is the import. Import Keychain from ‘react-native-keychain’ instead of Import * as Keychain …

Thanks @vonovak, your comment helped me to get it working with android. In addition to the rnpm link, I had to add to MainApplication.java:

...
import com.oblador.keychain.KeychainPackage; // <-- added
....

    protected List<ReactPackage> getPackages() {
      return Arrays.<ReactPackage>asList(
          new MainReactPackage(),
          new KeychainPackage() // <-- added

That fixed it for Android. Still need to sort out iOS though. FYI @oblador