[BREAKGLASS] Use SharedPreference (Android) and UserDefaults (iOS) with React Native over a unified interface
Go to file
Microsoft Provenance Contributions 05c53de380
Update package.json to include the repository
NOTE: This is not a bot. This change was made by and comments will be reviewed by humans. We are using this service account to be able to track the overall progress.

With the rise in supply chain attacks and OSS dependencies being used as a attack vector, Microsoft is working with our ecosystem partners, such as the Linux Foundation's OpenSSF, to enable OSS consumers to track packages back to their public sources. 

We've identified that the following packages published to NPM do not report where sources can be found, typically accomplished by including a link to your GitHub repository in your package.json REPOSITORY field. This PR was created to add this value, ensuring future releases will include this provenance information.

If you do not want us to create such PRs against your GitHub repositories, or wish to provide any other feedback, please comment on this PR. 

Published NPM packages with repository information:
• react-native-default-preference
2021-08-25 16:58:32 +08:00
android fix: use default shared preferences name on android 2021-05-21 16:24:27 -04:00
flow-typed Update types as get will return null if a key is not set on android 2021-05-26 09:53:52 +01:00
ios Clean up header file 2019-09-28 11:32:35 +02:00
.gitignore Initial commit 2016-12-08 12:14:15 +08:00
index.d.ts Update types as get will return null if a key is not set on android 2021-05-26 09:53:52 +01:00
index.js Initial commit 2016-12-08 12:14:15 +08:00
LICENSE Closes #11 2017-08-16 13:57:55 +08:00
package.json Update package.json to include the repository 2021-08-25 16:58:32 +08:00
README.md Update types as get will return null if a key is not set on android 2021-05-26 09:53:52 +01:00
RNDefaultPreference.podspec Fix Xcode 12 compatibility 2020-12-08 10:53:40 +10:00

npm version

react-native-default-preference

Use SharedPreferences (Android) and UserDefaults (iOS) with React Native over a unified interface. All data is stored as a string. If you need to support more complex data structures, use serialization/deserialization (e.g. JSON).

Getting started

$ npm install react-native-default-preference --save

React Native >= 0.60

$ cd ios && pod install

React Native <= 0.59

$ react-native link react-native-default-preference

Manual installation

iOS

  1. In XCode, in the project navigator, right click LibrariesAdd Files to [your project's name]
  2. Go to node_modulesreact-native-default-preference and add RNDefaultPreference.xcodeproj
  3. In XCode, in the project navigator, select your project. Add libRNDefaultPreference.a to your project's Build PhasesLink Binary With Libraries
  4. Run your project (Cmd+R)<

Android

  1. Open up android/app/src/main/java/[...]/MainApplication.java
  • Add import com.kevinresol.react_native_default_preference.RNDefaultPreferencePackage; to the imports at the top of the file
  • Add new RNDefaultPreferencePackage() to the list returned by the getPackages() method
  1. Append the following lines to android/settings.gradle:
    include ':react-native-default-preference'
    project(':react-native-default-preference').projectDir = new File(rootProject.projectDir, 	'../node_modules/react-native-default-preference/android')
    
  2. Insert the following lines inside the dependencies block in android/app/build.gradle:
      compile project(':react-native-default-preference')
    

Usage

import DefaultPreference from 'react-native-default-preference';

DefaultPreference.get('my key').then(function(value) {console.log(value)});
DefaultPreference.set('my key', 'my value').then(function() {console.log('done')});

API

function get(key: string): Promise<string | undefined | null>;
function set(key: string, value: string): Promise<void>;
function clear(key: string): Promise<void>;
function getMultiple(keys: string[]): Promise<string[]>;
function setMultiple(data: {[key: string]: string}): Promise<void>;
function clearMultiple(keys: string[]): Promise<void>;
function getAll(): Promise<{[key: string]: string}>;
function clearAll(): Promise<void>;

/** Gets and sets the current preferences file name (android) or user default suite name (ios) **/
function getName(): Promise<string>;
function setName(name: string): Promise<void>;

Cordova Native Storage Compatibility

This module is compatible with cordova-plugin-native-storage.

Android

You need to use the same SharedPreference as in the cordova plugin, to do so add the following line:

import { Platform } from 'react-native';
// ...
if (Platform.OS === 'android') DefaultPreference.setName('NativeStorage');

iOS

You don't need to change the name, as cordova-plugin-native-storage uses the default value.