2015-05-20 15:33:48 +00:00
# react-native-keychain
Keychain Access for React Native
2015-05-20 18:39:52 +00:00
2015-10-29 15:38:50 +00:00
Currently functionality is limited to just storing internet and generic passwords.
2015-05-20 18:39:52 +00:00
2016-09-18 14:30:07 +00:00
## ❗ Enable `Keychain Sharing` entitlement for iOS 10
For iOS 10 you'll need to enable the `Keychain Sharing` entitlement in the `Capabilities` section of your build target. (See screenshot). Otherwise you'll experience the error shown below.
![screen shot 2016-09-16 at 20 56 33 ](https://cloud.githubusercontent.com/assets/512692/18597833/15316342-7c50-11e6-92e7-781651e61563.png )
```
Error: {
code = "-34018";
domain = NSOSStatusErrorDomain;
message = "The operation couldn\U2019t be completed. (OSStatus error -34018.)";
}
```
2015-05-20 18:39:52 +00:00
## Installation
2016-08-26 06:40:09 +00:00
`$ npm install --save react-native-keychain`
2015-10-29 15:38:50 +00:00
### Option: Manually
2015-05-20 18:39:52 +00:00
* Right click on Libraries, select **Add files to "…"** and select `node_modules/react-native-keychain/RNKeychain.xcodeproj`
* Select your project and under **Build Phases** -> **Link Binary With Libraries** , press the + and select `libRNKeychain.a` .
2015-10-29 15:38:50 +00:00
### Option: With [CocoaPods](https://cocoapods.org/)
Add the following to your `Podfile` and run `pod update` :
```
pod 'RNKeychain', :path => 'node_modules/react-native-keychain'
```
2015-05-20 18:39:52 +00:00
2016-08-26 06:40:09 +00:00
### Option: With `react-native link`
2016-01-19 17:19:04 +00:00
2016-08-26 06:40:09 +00:00
`$ react-native link`
2016-01-19 17:19:04 +00:00
2015-05-20 18:39:52 +00:00
## Usage
2015-05-20 18:41:10 +00:00
See `KeychainExample` for fully working project example.
2015-05-20 18:39:52 +00:00
```js
2017-01-10 21:25:04 +00:00
import * as Keychain from 'react-native-keychain';
2015-05-20 18:39:52 +00:00
2017-01-10 22:12:20 +00:00
const username = 'zuck';
const password = 'poniesRgr8';
2015-05-29 16:25:56 +00:00
// Generic Password, service argument optional
Keychain
.setGenericPassword(username, password)
.then(function() {
console.log('Credentials saved successfully!');
});
2016-06-05 17:17:48 +00:00
// service argument optional
2015-05-29 16:25:56 +00:00
Keychain
.getGenericPassword()
.then(function(credentials) {
console.log('Credentials successfully loaded for user ' + credentials.username);
2016-06-08 21:21:06 +00:00
}).catch(function(error) {
console.log('Keychain couldn\'t be accessed! Maybe no value set?', error);
2015-05-29 16:25:56 +00:00
});
2016-06-05 17:17:48 +00:00
// service argument optional
2015-05-29 16:25:56 +00:00
Keychain
.resetGenericPassword()
.then(function() {
console.log('Credentials successfully deleted');
});
// Internet Password, server argument required
2017-01-10 22:12:20 +00:00
const server = 'http://facebook.com';
2015-05-20 18:39:52 +00:00
Keychain
.setInternetCredentials(server, username, password)
.then(function() {
2015-05-29 16:25:56 +00:00
console.log('Credentials saved successfully!');
2015-05-20 18:39:52 +00:00
});
Keychain
.getInternetCredentials(server)
.then(function(credentials) {
2017-02-10 09:58:39 +00:00
if (credentials) {
console.log('Credentials successfully loaded for user ' + credentials.username);
}
2015-05-20 18:39:52 +00:00
});
Keychain
.resetInternetCredentials(server)
2015-05-29 16:25:56 +00:00
.then(function() {
console.log('Credentials successfully deleted');
2015-05-20 18:39:52 +00:00
});
2017-01-10 21:25:04 +00:00
Keychain
.requestSharedWebCredentials()
.then(function(credentials) {
if (credentials) {
console.log('Shared web credentials successfully loaded for user ' + credentials.username);
}
})
2017-01-10 22:12:20 +00:00
Keychain
.setSharedWebCredentials(server, username, password)
.then(function() {
console.log('Shared web credentials saved successfully!');
})
2015-05-20 18:39:52 +00:00
```
2016-05-31 22:25:51 +00:00
### Android
2016-08-26 06:40:09 +00:00
### Option: With `react-native link`
`$ react-native link` and check MainApplication.java to verify the package was added.
2016-05-31 22:25:51 +00:00
* Note: Android support requires React Native 0.19 or later
2016-06-05 17:17:48 +00:00
* on Android, the `setInternetCredentials(server, username, password)` call will be resolved as call to `setGenericPassword(username, password, server)` and the data will be saved in `SharedPreferences` , encrypted using Facebook conceal. Use the `server` argument to distinguish between multiple entries.
2016-05-31 22:25:51 +00:00
2016-08-26 06:40:09 +00:00
### Option: Manually
2016-05-31 22:25:51 +00:00
* Edit `android/settings.gradle` to look like this (without the +):
```diff
rootProject.name = 'MyApp'
include ':app'
+ include ':react-native-keychain'
+ project(':react-native-keychain').projectDir = new File(rootProject.projectDir, '../node_modules/react-native-keychain/android')
```
* Edit `android/app/build.gradle` (note: **app** folder) to look like this:
```diff
apply plugin: 'com.android.application'
android {
...
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.android.support:appcompat-v7:23.0.1'
compile 'com.facebook.react:react-native:0.19.+'
+ compile project(':react-native-keychain')
}
```
2017-01-10 21:56:19 +00:00
* Edit your `MainApplication.java` (deep in `android/app/src/main/java/...` ) to look like this (note **two** places to edit):
2016-05-31 22:25:51 +00:00
```diff
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()
);
}
...
}
```
2015-05-20 18:39:52 +00:00
## License
2017-01-10 22:12:20 +00:00
MIT © Joel Arvidsson 2016-2017