# react-native-default-preference **Repository Path**: mirrors_Marus/react-native-default-preference ## Basic Information - **Project Name**: react-native-default-preference - **Description**: Use SharedPreference (Android) and UserDefaults (iOS) with React Native over a unified interface - **Primary Language**: Unknown - **License**: MIT - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 0 - **Created**: 2020-08-09 - **Last Updated**: 2026-02-28 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README # react-native-default-preference Use `SharedPreference` (Android) and `UserDefaults` (iOS) with React Native over a unified interface. All data are stored as String, in case more complex data structure is needed, serialize it (e.g. JSON) ## Getting started `$ npm install react-native-default-preference --save` ### Mostly automatic installation `$ react-native link react-native-default-preference` ### Manual installation #### iOS 1. In XCode, in the project navigator, right click `Libraries` ➜ `Add Files to [your project's name]` 2. Go to `node_modules` ➜ `react-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 Phases` ➜ `Link Binary With Libraries` 4. Run your project (`Cmd+R`)< #### Android 1. Open up `android/app/src/main/java/[...]/MainApplication.java` - Add `import com.reactnativedefaultpreference.RNDefaultPreferencePackage;` to the imports at the top of the file - Add `new RNDefaultPreferencePackage()` to the list returned by the `getPackages()` method 2. 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') ``` 3. Insert the following lines inside the dependencies block in `android/app/build.gradle`: ``` compile project(':react-native-default-preference') ``` ## Usage ```javascript 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 ```haxe function get(key:String):Promise; function set(key:String, value:String):Promise; function clear(key:String):Promise; function getMultiple(keys:Array):Promise>; function setMultiple(data:Object):Promise; function clearMultiple(keys:Array):Promise; function getAll():Promise; function clearAll():Promise; /** Gets and sets the current preferences file name (android) or user default suite name (ios) **/ function getName():Promise; function setName(name:String):Promise; ``` ## 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: ```js 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.