# react-native-default-preference

> Use SharedPreference (Android) and UserDefaults (iOS) with React Native over a unified interface

Latest version **1.4.4** (published 2022-04-07) · MIT license · 0 weekly downloads

## Install

```sh
npm install react-native-default-preference
pnpm add react-native-default-preference
yarn add react-native-default-preference
bun add react-native-default-preference
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.4.4 |
| Published | 2022-04-07 |
| First published | 2016-12-08 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 0 |
| Unpacked size | 87 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 276 |
| Author | kevinresol |
| Maintainers | kevinresol, marcelkalveram |
| Keywords | react-native, NSUserDefaults, user defaults, SharedPreferences, shared preferences |

## Links

- npm: https://www.npmjs.com/package/react-native-default-preference
- Repository: https://github.com/kevinresol/react-native-default-preference
- Homepage: https://github.com/kevinresol/react-native-default-preference#readme
- Issues: https://github.com/kevinresol/react-native-default-preference/issues
- npm.io page: https://npm.io/package/react-native-default-preference

## Recent versions

- 1.4.4 (latest) — 2022-04-07
- 1.4.3 — 2020-05-16
- 1.4.2 — 2020-03-17
- 1.4.1 — 2019-10-10
- 1.4.0 — 2019-07-10
- 1.3.2 — 2018-10-09
- 1.3.1 — 2018-01-07
- 1.3.0 — 2018-01-05
- 1.2.1 — 2018-01-01
- 1.2.0 — 2017-10-11
- 1.1.1 — 2017-04-25
- 1.1.0 — 2017-04-25
- 1.0.2 — 2017-03-14
- 1.0.1 — 2017-02-28
- 1.0.0 — 2016-12-08

## README

[![npm version](https://badge.fury.io/js/react-native-default-preference.svg)](https://badge.fury.io/js/react-native-default-preference)

# 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 `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.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
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

```typescript
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:

```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.

---
_Source: https://npm.io/package/react-native-default-preference · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
