react-native-sensitive-info is the next generation of react-native-get-shared-prefs.
react-native-sensitive-info
manages all data stored in Android Shared Preferences, iOS Keychain and Windows Credentials. You can set and get all key/value using simple methods.
RN SensitiveInfo Version | Description |
---|---|
4.0+ | Compatible with RN 0.40+ |
<= 3.0.2 | Compatible with RN 0.40 or below |
Install react-native-sensitive-info
using:
npm i -S react-native-sensitive-info
or yarn add react-native-sensitive-info
react-native link react-native-sensitive-info
In XCode, in the project navigator:
- Right click Libraries
- Add Files to [your project's name]
- Go to node_modules/react-native-sensitive-info
- Add the .xcodeproj file
In XCode, in the project navigator, select your project.
- Add the libRNSensitiveInfo.a from the RNSensitiveInfo project to your project's Build Phases ➜ Link Binary With Libraries
- Click .xcodeproj file you added before in the project navigator and go the Build Settings tab. Make sure 'All' is toggled on (instead of 'Basic').
- Look for Header Search Paths and make sure it contains both
$(SRCROOT)/../react-native/React and $ (SRCROOT)/../../React - mark both as recursive. (Should be OK by default.)
Run your project (Cmd+R)
Go to settings.gradle
inside your android project folder and paste this lines there:
include ':react-native-sensitive-info'
project(':react-native-sensitive-info').projectDir = new File(rootProject.projectDir, '../node_modules/react-native-sensitive-info/android')
and paste it into build.gradle
:
compile project(':react-native-sensitive-info')
In your MainApplication.java
add:
import br.com.classapp.RNSensitiveInfo.RNSensitiveInfoPackage; //<- You must import this
protected List<ReactPackage> getPackages() {
return Arrays.<ReactPackage>asList(
new MainReactPackage(),
new RNSensitiveInfoPackage(), // <- Add this line
);
}
Sync gradle and go :)
-
Open the solution in Visual Studio for your Windows apps.
-
Right click your in the Explorer and click Add > Existing Project....
-
Navigate to .//node_modules/react-native-sensitive-info/windows/RNSensitiveInfo/RNSensitiveInfo/ and add RNSensitiveInfo.csproj.
-
Right click on your React Native Windows app under your solutions directory and click Add > Reference....
-
Check the RNSensitiveInfo you just added and press Ok
-
Open MainPage.cs in your app
using RNSqlite2;
get
{
return new List<IReactPackage>
{
new MainReactPackage(),
new RNSensitiveInfoPackage(),
};
}
We unified our library's methods to bring more efficiency and simplify the usability for other developers. We hope that you enjoy it. :)
setItem(key, value, options)
: You can insert data into shared preferences & keychain using this promise method.
getItem(key, options)
: This promise will get value from given key.
deleteItem(key, options)
: It will delete value from given key
getAllItems(options)
: Will retrieve all keys and values from Shared Preferences & Keychain
"Options" is a new parameter (optional) that you can pass to our methods. But what does it do? Now, you can select which keychain's service (iOS) and shared preferences's name (android) you can use. To do so:
SInfo.setItem('key1', 'value1', {
sharedPreferencesName: 'mySharedPrefs',
keychainService: 'myKeychain'});
But if you prefer to not use it, our default sharedPreferencesName is: shared_preferences and keychainService is: app. For that, use:
SInfo.setItem('key1', 'value1', {});
If you used Android's getDefaultSharedPreferences in your project the shared preference's name that you are looking for is: com.mypackage.MyApp_preferences. On the other hand if you used iOS's Keychain the default service is: app which is our default too.
Here is a simple example:
import SInfo from 'react-native-sensitive-info';
SInfo.setItem('key1', 'value1', {
sharedPreferencesName: 'mySharedPrefs',
keychainService: 'myKeychain'
}).then((value) =>
console.log(value) //value 1
);
SInfo.setItem('key2', 'value2', {});
SInfo.getItem('key1', {
sharedPreferencesName: 'mySharedPrefs',
keychainService: 'myKeychain'}).then(value => {
console.log(value) //value1
});
SInfo.getItem('key2',{}).then(value => {
console.log(value) //value2
});
SInfo.getAllItems({
sharedPreferencesName: 'mySharedPrefs',
keychainService: 'myKeychain'}).then(values => {
console.log(values) //value1, value2
});
As jailbroken device can access your iOS Keychain/ Android shared preference and key store in plain text, it is necessary to add another layer of protection so even jailbreaking won't leak your data (like refresh_token or bank account password).
- for iOS it is implemented though Access Control. Everytime when app wants to access the protected keychain item, a prompt by iOS will show up. Only when authentication success will the app get the keychain item.
- for Android it is implemented though FingerprintManager + Keystore. Keystore has a function called
setUserAuthenticationRequired
which makes Keystore requires user authentication before getting value. However Android doesn't nicely user to scan their finger, it just throws error. Here is where FingerprintManager comes in. However (AGAIN) FingerprintManager doesn't show prompt for you, so you need to build UI yourself to let user to know that it is time to scan fingerprint.
The example in the repo shows how to use this feature and how to build some Android UI based on callbacks.
NOTE: fingerprint will only work with Android 6.0 and above.
HELP NEEDED: It will be nice if someone can build an Android native prompt to make Android touch as easy to use as iOS. Maybe we can borrow some code from google's example
If you would like to use redux-persist to store information from your Redux state into secure storage, you can use redux-persist-sensitive-storage, which provides a custom storage back-end for redux-persist that uses react-native-sensitive-info.
Pull requests are always welcome :)